1

If I want to execute a Java application programmatically and I am in the same directory of the file [NewFile] I can simply use this method

try {
     String line;
     Process p2 = Runtime.getRuntime().exec( "java NewFile" );
     BufferedReader in = new BufferedReader(
         new InputStreamReader(p2.getInputStream()) );
     while ((line = in.readLine()) != null) {
         System.out.println(line);
     }
     in.close();
 }
 catch (Exception e) {
       // ...
 }

But the problem is when I'm not in the directory of the file [NewFile]. I tried Process p2 = Runtime.getRuntime().exec( "java /users/Documents/project/NewFile" ); but it's not working.

1
  • Use a ProcessBuilder. Runtime.exec() should not be used. Commented Jun 16, 2013 at 23:29

1 Answer 1

3

You should probably include folder with your classes to classpath. Could you try with

java -cp /users/Documents/project NewFile
Sign up to request clarification or add additional context in comments.

4 Comments

@user2229953: if Pshemo's answer solved your problem, you will want to "accept" it by clicking on the check mark to the left of it.
How can we give more memory space to jvm with this way?
@huseyintugrulbuyukisik: yours is a "hijack" question. But if you mean the JVM that is making the call above -- you can't add more memory. If you mean the JVM that will be creating the new Java application, then you do it the same way as for any Java application, by adding the appropriate command line parameters.
@user2229953 I am not sure if original JVM which runs this code will allow you to take lot of memory but you can try adding -Xms512m -Xmx1024m (512MB is initial heap size, 1024MB is max) options to command line, lets say before NewFile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.