Reputation: 1405
Hi I have program which executed another 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 is = proc.getInputStream();
byte b[] = new byte[is.available()];
is.read( b, 0, b.length );
System.out.println( new String( b ) );
}
}
and test.jar definition is like this :
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 is to get the running path of test.jar, but as test.jar was executed by another MainTestClass jar, System.getProperty( "user.dir" )
and new File( "" ).getAbsolutePath()
both are returning MainTestClass running path. Is there any way to get the child process running path inside test.jar itself.
Upvotes: 0
Views: 47
Reputation: 7461
You're confusing working directory (CWD) and basedir of running application (is that what you call "running path"?). You can run application inside any CWD, to do so with Java you 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, you should avoid depending on CWD, because user can run your jar from any directory.
Upvotes: 3