Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am trying to run a executable file generated by c in Java. Originally, I can run it on terminal with

./bin/svm-train -s 0 -t 0 -d 3 -g 0.0 -r 0.0 -n 0.5 -m 40.0 -c 1.0 -e 0.001 -p 0.1 ./data/trainfile ./model/update.model

This command on terminal works well and it will take a few seconds to generate a file 'update.model' as indicated in the code.

But when I try to put this process in Java with the following code, the program ends without generating 'update.model'

String[] cmdUpdateTrain = new String[]{"/bin/bash", "-c", "./bin/svm-train -s 0 -t 0 -d 3 -g 0.0 -r 0.0 -n 0.5 -m 40.0 -c 1.0 -e 0.001 -p 0.1 ./data/trainfile ./model/update.model"};
Runtime.getRuntime().exec(cmdUpdateTrain);

If I try with the following code with Java, it works fine the model can also be generated.

String[] cmdUpdateTrain = new String[]{"/bin/bash", "-c", "./bin/svm-train ./data/trainfile ./model/update.model"};
Runtime.getRuntime().exec(cmdUpdateTrain);

So I think it may be the problem of handling parameter for ./bin/svm-train.


I have found why it happens. It is because the program ends before the execution of ./bin/svm-train stops. And the following code fix this problem.

try{
    String[] cmdUpdateTrain = new String[]{"/bin/bash", "-c", "./bin/svm-train -s 0 -t 0 -d 3 -g 0.0 -r 0.0 -n 0.5 -m 40.0 -c 1.0 -e 0.001 -p 0.1 ./data/trainfile ./model/update.model"};
    Process psTrain = Runtime.getRuntime().exec(cmdUpdateTrain);
    psTrain.waitFor();
}catch(InterruptedException interupt){
}
share|improve this question
1  
You say it doesn't work. What happens when you try to run it? Do you get any error messages? Please edit your question to include this information. – Kenster Feb 8 at 13:05

1 Answer 1

Runtime.getRuntime().exec(cmdUpdateTrain)

Don't. Use. Runtime.exec().

You have several problems with your command line.

First of all: did you notice the beginning of your command, and the paths of your two file arguments? They are relative to the directory in which you launch your command; this is highly unlikely that this is the same directory from which your Java program is launched, so just for that it can only fail.

Second: you use a command interpreter. Why?

Third: how about stdin, stdout, stderr? Does your program output anything? If yes, how do you handle this?

The solution is, as always when running external processes in Java, to use a ProcessBuilder:

final Path basedir = Paths.get("whereyourprogramislaunched");
final Path stdout = Paths.get("somewheretooutputstdout");
final Path stderr = Paths.get("somewheretooutputstderr");

final ProcessBuilder builder = new ProcessBuilder(
    "./bin/svm-train", "-s", "0", "-t", "0", "etc", "etc"
);

pb.directory(basedir.toFile());
pb.redirectOutput(stdout.toFile());
pb.redirectError(stderr.toFile());

final Process process = builder.start();

// Deal with process exit code
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.