1
java -jar twitie_tag.jar <path to model file> <path to input file> <output file>

I have given the above command in java process builder like given below

ProcessBuilder pb = new ProcessBuilder("java","-jar","twitie_tag.jar","models\\gate-EN-twitter.model", "newfile.txt"); 
pb.redirectErrorStream(true);
final Process process = pb.start();

InputStream stderr = process.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
          System.out.println(line);
}

process.waitFor();
System.out.println("Waiting ...");
System.out.println("Returned Value :" + process.exitValue());

but it is throwing an error as Exception in thread "main"

java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

if i give redirectoutputstream it is not prining anything it is just printing waiting and the exit value is 1

i have a jar file twittejar and i executed in command line as java, -jar, twitie_tag.jar, gate-EN-twitter.model, newfile.txt, > outfile.txt (output will be saved in outfile.txt). if i give java, -jar, twitie_tag.jar, gate-EN-twitter.model, newfile.txt (the output will be shown in that command window itself) but i need to execute theses commands using java program.

azro
  • 53,056
  • 7
  • 34
  • 70
vinay
  • 25
  • 2
  • 8
  • Vinay can you please paste the complete code. – Kick Apr 03 '15 at 11:32
  • please do check i pasted my code above – vinay Apr 03 '15 at 13:14
  • It is a while since I did this but I remember having to use a separate thread to read the input stream like this. Try `redirectOutput(ProcessBuilder.Redirect.INHERIT)` instead. – redge Apr 03 '15 at 13:53

4 Answers4

0

On Linux or MacOSX

private void runProcess(String jarFolder, String command) {
        Process pr = null;
        try {
            pr = Runtime.getRuntime().exec(new String[] {
                    "/bin/sh", 
                    "-c", 
                    "cd " + jarFolder + " ; " + command
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

(On Windows you use the same approach, but using "cmd.exe" with appropriate arguments.)

Call it with:

runProcess(jarFolder, 
           "java -cp youJarFile packages.MainClass " + 
           param1 + " " + param2);
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
MihaiC
  • 1,618
  • 1
  • 10
  • 14
  • `Runtime.exec()` is a big step backwards from `ProcessBuilder`. http://stackoverflow.com/questions/10723346/why-should-avoid-using-runtime-exec-in-java – Andy Turner Apr 03 '15 at 11:27
0

Then problem is that you do not have the output or error streams of the process that is running the Java command. You may need to redirect the output of the process using the redirectOutput and redirectError methods so that you can see if there is a problem with the shell that is running your jar file.

redge
  • 1,182
  • 7
  • 6
  • ya thanks. i used redirecterror i got to know the error and i used redirectoutput for output but it is not showing anything – vinay Apr 03 '15 at 12:59
  • i have a jar file twittejar and i executed in command line as java, -jar, twitie_tag.jar, gate-EN-twitter.model, newfile.txt, > outfile.txt (output will be saved in outfile.txt). if i give java, -jar, twitie_tag.jar, gate-EN-twitter.model, newfile.txt (the output will be shown in that command window itself) but i need to execute theses commands using java program. would you please help us – vinay Apr 03 '15 at 13:03
0

Are you even ".start()"ing the process ? ProcessBuilder only accumulates the conditions under which you want to start the process under, you need to invoke .start() on the builder object to actually run the process and give you back a Process object on which you can .waitFor() completion.

See http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html as there is a good example in the description.

DarkRift
  • 224
  • 2
  • 11
0

File file = new File("errorlog.txt");

        ProcessBuilder pb = new ProcessBuilder("java","-jar","twitie_tag.jar","models//gate-EN-twitter.model", "newfile.txt");
        pb.redirectOutput(file);
        final Process process = pb.start();

        InputStream stderr = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;

       while ((line = br.readLine()) != null) {
         System.out.println(line);
       }
      process.waitFor();
      System.out.println("Waiting ...");
      System.out.println("Returned Value :" + process.exitValue());

Thank you for all..who answered for my question

vinay
  • 25
  • 2
  • 8