When you run the java program, you should be in the root directory of the project and run java package.to.ClassWhichContainsMainMethod
Runtime.getRuntime().exec() will give you a Process that contains the OutputStream and InpuStream in the executed application.
You can redirect the contents of InputStream to a log file.
In your case, I would use this exec: public Process exec(String command, String[] envp, File dir) as follows:
exec("java HelloWorld", null, new File("C:/"));
Copy data from inputStream file to file (code is stolen on this message ):
public runningMethod(){ Process p = exec("java HelloWorld", null, new File("C:/")); pipe(p.getInputStream(), new FileOutputStream("C:/test.txt")); } public void pipe(InputStream in, OutputStream out) { byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; int writtenBytes; while((writtenBytes = in.read(buf)) >= 0) { out.write(buf, 0, writtenBytes); } }
source share