Java-Getting running file location of a child process -
hi have program executed jar file
public class maintestclass { public static void main( string[] args ) throws ioexception, interruptedexception { process proc = runtime.getruntime().exec( "java -jar c:\\users\\rijo\\desktop\\test.jar" ); proc.waitfor(); java.io.inputstream = proc.getinputstream(); byte b[] = new byte[is.available()]; is.read( b, 0, b.length ); system.out.println( new string( b ) ); } }
and test.jar definition :
public class testmain { public static void main( string[] args ) { system.out.println( "started" ); system.out.println( "path : " + new file( "" ).getabsolutepath() ); system.out.println( "end " ); system.out.println( system.getproperty( "user.dir" ) + " path" ); } }
my intention running path of test.jar, test.jar executed maintestclass jar, system.getproperty( "user.dir" )
, new file( "" ).getabsolutepath()
both returning maintestclass running path. there way child process running path inside test.jar itself.
you're confusing working directory (cwd) , basedir of running application (is call "running path"?). can run application inside cwd, java can use processbuilder
:
string jar = "c:\\users\\rijo\\desktop\\test.jar"; process proc = new processbuilder() .command("java", "-jar", jar) .directory(new file(jar).getparentfile()) .start();
actually, should avoid depending on cwd, because user can run jar directory.
Comments
Post a Comment