Running executable from java fails - java

I am trying to run mdb-export on a file I know exist in that directory. But it does not seem to execute. "ls-l" will so I am sure that the java code is working. The command will execute perfectly from bash.
The failing command is
/usr/bin/mdb-export -Q -d ';' -D '%Y-%m-%d %H:%M:%S' /home/jocke/viking.mdb resultat >> resultat.csv
private void runCommand() {
try {
String workingdirectory=System.getProperty("user.dir");
Runtime runtime = Runtime.getRuntime();
//Process process = runtime.exec("/usr/bin/mdb-export -Q -d ';' -D '%Y-%m-%d %H:%M:%S' /home/jocke/viking.mdb resultat >> resultat.csv");
Process process = runtime.exec("/usr/bin/mdb-export /home/jocke/viking.mdb resultat >> resultat.csv");
//
process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
}

You cannot use output redirection this way. Use a ProcessBuilder instead:
ProcessBuilder pb = new ProcessBuilder("/usr/bin/mdb-export", "/home/jocke/viking.mdb", "resultat");
File csv = new File("resultat.csv");
pb.redirectOutput(Redirect.appendTo(csv);
Process p = pb.start();

Related

Java does not execute one sh file, and never ends

I'm trying to execute one sh file that run postgres commands, but it never ends.
The sh file is that:
#!/bin/bash
echo "My ARGUMENTS $1 $2 $3 $4";
psql -h $1 -p $2 -U $3 -d template1 -c "drop database IF EXISTS $4";
psql -h $1 -p $2 -U $3 -d template1 -c "create database $4";
echo "OPERATION COMPLETED";
And my java method is the following:
public void createDB() {
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command("sh", "-c", "ls");
ProcessBuilder pb = new ProcessBuilder("/tmp/test.sh", "localhost", "5432", "postgres", "newDB");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}catch (Exception e) {
System.out.println("ERORR");
}
}
the output is:
My ARGUMENTS localhost 5432 postgres newDB
But never execute the second psql statement, can someone help me please.
The soluction was the following:
public void createDB() {
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command("sh", "-c", "ls");
ProcessBuilder pb = new ProcessBuilder("/tmp/test.sh", "localhost", "5432", "postgres", "newDB");
// set environment variable password
env.put("PGPASSWORD", "123456");
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
}catch (Exception e) {
System.out.println("ERORR");
}
}
I pass the password by enviroment variable. Thanks to all!

Running docker-compose command in Java application

I'm trying to run
docker-compose up -d
in Java application. Here's my code:
List<String> commands = new ArrayList<>();
commands.add("zsh");
commands.add("-c");
commands.add("docker-compose up --build");
Process dockerComposeCommand;
ProcessBuilder pb = new ProcessBuilder();
pb.command(commands);
pb.directory(new File(server_config.getDOCKERFILE_PATH()));
String path = System.getenv("PATH");
pb.environment().put("PATH","/usr/bin:"+path);
pb.redirectErrorStream(true);
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
List<String> result = new ArrayList<>();
try {
dockerComposeCommand = pb.start();
dockerComposeCommand.waitFor();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(dockerComposeCommand.getInputStream()))) {
String line = reader.readLine();
while (line != null) {
result.add(line);
line = reader.readLine();
System.out.println(line);
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
However, the result was
zsh:1: command not found: docker-compose
I've checked my .zshrc file and add
export PATH="$PATH:/usr/local/bin"
to include docker-compose's path.
Did I miss something or docker-compose just can't simply execute by Java?
Any advice would be helpful!

Curl command through Java works in windows and not in linux

I am trying to execute a curl command using Java using the code below
String myUrl= "https://someIp:somePort";
String username = "someusername";
String password = "somepassword";
String command = "curl -k -d \"client_id=someId\" -d \"username="+username+"\" -d \"password="+password+"\" -d \"grant_type=password\" -d \"client_secret=\" \""+myUrl+"/myauth/openid-connect/token\"";
Process process = Runtime.getRuntime().exec(command);
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = process.getInputStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}
String response = result.toString(StandardCharsets.UTF_8.name());
This works on windows machine but not on linux. Is there any difference between linux and windows in the way curl command is executed using exec method ?
Both execution are done using same JRE. On windows I get the token successfully but in Linux I get the following response :
Response = {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
Thank you
After investigation it seems that when someone executes this java code in linux enviroment the curl command is not properly constructed.
I used the following code and everything worked fine :
String cUrlToKeyCloak = "curl -k -d \"client_id=someId\" -d \"username="+username+"\" -d \"password="+password+"\" -d \"grant_type=password\" "+keyCloakUrl+"/auth/realms/master/protocol/openid-connect/token";
ProcessBuilder processBuilder = new ProcessBuilder();
if(!System.getProperty("os.name").contains("Windows"))
processBuilder.command("bash", "-c", cUrlToKeyCloak );
else
processBuilder.command("cmd.exe", "/c", cUrlToKeyCloak );
String cKeyResponse = "";
try {
Process process = processBuilder.start();
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
LOGGER.info("Curl command to keyCloak requested ...");
LOGGER.info("cKey response = "+output);
cKeyResponse = output.toString();
} else {
LOGGER.error("Curl command to keyCloak executed with error ...");
LOGGER.info("cKey response = "+output);
return false;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}

How can I run command from Java code and read the output?

I am writing a code to execute a command and reading the output
If I run the command on command prompt, it looks like this
Command is
echo 'excellent. awesome' | java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -stdin
Command produces multi line output. How can I print this output in my java code?
I have written following code, but it produces output as command itself that is
echo 'excellent. awesome' | java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -stdin
rather than actual command output as we can see it in the screenshot
final String cmd = "java -cp \"*\" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -stdin";
final String path = "C:/Project/stanford-corenlp-full-2015-01-29/stanford-corenlp-full-2015-01-29";
String input = "excellent";
String cmdString = "echo '" +input + "' | " + cmd;
Process process = Runtime.getRuntime().exec(cmdString,null, new File(path));
process.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
Try using ProcessBuilder:
try {
ProcessBuilder pb = new ProcessBuilder("your command here");
pb.redirectErrorStream(true);
Process p = pb.start();
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
} catch (InterruptedException e) {
//handle exception
}

Error: Cannot open VM: "[datastore] myVM1/myVM1.vmx", unknown file suffix

I'm having a lot of difficulty running vmrun through ProcessBuilder in Java.
I have a command string like this:
java -cp . RunTest 'vmrun -T esx -h https://10.123.236.123:443/sdk -u root -p password revertToSnapshot "[datastore] myVM1/myVM1.vmx" snapshot1'
When you run the vmrun command above (without Java), the command executes successfully. But with Java, I receive the ff. error:
Error: Cannot open VM: "[datastore] myVM1/myVM1.vmx", unknown file suffix
The code is below:
public static void main(String args[]) {
runProcessBuilderMulti(args[0]);
}
static void runProcessBuilderMulti (String cmd){
List<String> list = new ArrayList<String>();
System.out.println("Running Command: "+cmd.replace("\"","\\\""));
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(cmd);
while (m.find())
list.add(m.group(1));
System.out.println(list);
try {
Process process = new ProcessBuilder(list).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println(process.exitValue());
} catch (Exception e) {}
}
I used Runtime.exec() before switching to ProcessBuilder. I thought it had to do with the quotes, so I added the cmd.replace, but apparently it was a different issue.
Any help would be appreciated. Thanks!
I was playing around with ProcessBuilder when I learned it had trouble with piped commands using the above script. A bit more Googling found that ProcessBuilder needed to open a shell to execute certain commands.
ProcessBuilder b = new ProcessBuilder("/bin/sh", "-c", "ls -l | grep daemon");
The function is updated as below:
static void runProcessBuilderMulti (String cmd){
System.out.println("Running Command: "+cmd.replace("\"","\\\""));
try {
Process process = new ProcessBuilder("/bin/sh", "-c", cmd).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
System.out.println(process.exitValue());
} catch (Exception e) {}
}
I'm guessing that longer commands are more difficult to process, like piped commands, so it needs its own shell for execution.

Categories

Resources