As a part of my program, I have a connections manager that receives a connection from a client, and then gives the client a port number and a password to use to connect. A this point the manager needs to call the jar file I have to handle this connections, with a few arguments, and continue on,( ignoring what the other program is doing).
My problem has been with executing the jar file. I've looked up similar question's and have tried using a process builder and using Runtime.exec. I moved around the jar file, and checked it's permissions. It just refuse to work from another java program, but works perfectly from the command line. Here's an example of one of my test runs.
package test;
import java.io.*;
public class Main {
public static void main (String [] args ) throws IOException, ClassNotFoundException, InterruptedException {
Process p = Runtime.getRuntime().exec("java -jar \'/home/ryan/CytoscapeInterface.jar" +
"\' arg1 arg2");
//Process builder way
/*ProcessBuilder pb = new ProcessBuilder("/home/ryan/CytoscapeInterface.jar",
"-jar", "CytoscapeInterface.jar", "agr1", "arg2");
pb.redirectErrorStream();
Process p = pb.start();*/
BufferedInputStream bis = new BufferedInputStream(p.getErrorStream());
synchronized (p) { p.waitFor(); }
System.out.println(p.exitValue());//its 1 for runtime, 2 for process Builder
int read = bis.available();
//had a loop, found out I just needed to go through once
byte[] b = new byte [read];
bis.read(b);
read = bis.available();
bis.close();
FileOutputStream fos = new FileOutputStream (new File("/home/ryan/Desktop/FileTest.txt"));
fos.write(b);//Writes error file
fos.close();
}
}
waitFor returns 1 for runtime and 2 for the builder. The error output for runtime is "Unable to access jarfile '/home/ryan/CytoscapeInterface.jar'. While using the builder gives a couple lines of error that had some weird characters, the first error was command not found.