1

I want to run java file using 'java filename' command when you're not in the file's directly.

In the terminal we use :

cd filepath

java filename

but in Eclipse you cannot change the directory using 'cd' so how can I run the file although I can't change the directory

I am using this method to run a command using Java

try {
           String line;
           Process p = Runtime.getRuntime().exec( "cd /Users/apple/Documents/Documents/workspace/UserTesting/src" );
           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) {
           // ...
         }

Here are several trials

Apples-MacBook-Pro:~ apple$ cd /Users/apple/Documents/Documents/workspace/UserTesting/src Apples-MacBook-Pro:src apple$ java NewFile 5 90 35 45 150 3
Reichweite---- nach blase art
3 5 35 45 90 150

Apples-MacBook-Pro:src apple$ java /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile Exception in thread "main" java.lang.NoClassDefFoundError: /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile Caused by: java.lang.ClassNotFoundException: .Users.apple.Documents.Documents.workspace.UserTesting.src.NewFile at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Apples-MacBook-Pro:src apple$ java /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile.java Exception in thread "main" java.lang.NoClassDefFoundError: /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile/java Caused by: java.lang.ClassNotFoundException: .Users.apple.Documents.Documents.workspace.UserTesting.src.NewFile.java at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) Apples-MacBook-Pro:src apple$ Blockquote

student1234
  • 201
  • 1
  • 8

1 Answers1

1

This will not work.

For one, cd is a shell command. And then you create another process to run the java command.

You need a ProcessBuilder. You can set up the initial directory, environment etc:

final File wantedCwd = new File(...);
final ProcessBuilder pb = new ProcessBuilder("java", "thefile");

// Change directory
pb.directory(wantedCwd);

You can even change the stdin, stdout, stderr etc of the command.

DO NOT use Runtime.exec(). It won't (exec()) in many situations.

fge
  • 119,121
  • 33
  • 254
  • 329