-1

I am trying to run a java file using the terminal but from java. Meaning, I'll run the command using java. I am trying to execute the command 'cd /Users/apple/Documents/Documents/workspace/UserTesting/src ' that redirects to the following directory and then execute the command 'ls' that lists all the files in the current directory

I am using this method to run the Java file 'NewFile.java'

 try {
           String line;
           Process p = Runtime.getRuntime().exec( "cd /Users/apple/Documents/Documents/workspace/UserTesting/src" );
           Process p2 = Runtime.getRuntime().exec( "ls" );

           BufferedReader in = new BufferedReader(
                   new InputStreamReader(p2.getInputStream()) );
           while ((line = in.readLine()) != null) {
             System.out.println(line);
           }
           in.close();

     }
     catch (Exception e) {
           // ...
         }

The output

Directly using the terminal -> It gives 'NewFile.java'

Using this method using Java -> It always give 'bin' and 'src' for whatever command given to p2

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
  • Too difficult to understand your problem. You need to do present it better – Juned Ahsan Jun 16 '13 at 15:55
  • Here is a much better explanation @JunedAhsan – student1234 Jun 16 '13 at 15:59
  • @BrianRoach It is different, here I want to run a Java file from inside java using the commands we use in the terminal – student1234 Jun 16 '13 at 15:59
  • Which the answer to that question explains. Your process *is not a terminal*. You can't change the working directory by exec'ing an external process. And that's ignoring that exec'ing `ls` instead of simply reading the directory is rather silly in the first place. – Brian Roach Jun 16 '13 at 16:03
  • Your question does not make much sense. If you want to execute a java file why the cd/ls? Why not just `java /path/to/file.class`? Also this program should not even run correctly (`cd` is not an application), so it should not print anything at all. – Salem Jun 16 '13 at 16:05
  • because simply this doesn't work ! – student1234 Jun 16 '13 at 16:06
  • Even in the terminal you have to redirect first and then 'java FileName' – student1234 Jun 16 '13 at 16:07

1 Answers1

1

So, it seems the problem you're having is that you don't understand why you get different results when you invoke the program in different ways.

Here's what's going on: Runtime.geRuntime().exec() creates a new process, which is a child of the parent. Every process has its own working directory; when you fork a new process, it inherits the working directory of the parent. Invoking cd will then change the working directory of the current process (and this is a shell builtin, but ignore that for now and I'll get to it later).

So what you're doing is this:

Parent

-> create child 1 -> change working directory of child 1

-> create child 2 -> invoke "ls"

Note that child 2 will inherit the working directory of its parent. It won't know anything about the working directory of child 1. So depending on the working directory of the process that is invoking this method (in your case, either the terminal or...I don't know, your JDK install?) you will get different results.

If you want the same results every time, you could do something like this:

Process p = Runtime.getRuntime().exec( "ls /Users/apple/Documents/Documents/workspace/UserTesting/src" );

And if you want to be able to exec your program from anywhere, just use the full path:

Process p = Runtime.getRuntime().exec( "java /Users/apple/Documents/Documents/workspace/UserTesting/NewFile" );

(assuming, of course, that you have already used javac to build NewFile.class in that directory, and that you have the right permissions to execute it.)

Re: cd, as I mentioned before this is a command that's built into your shell. When you invoke the command using exec in this way, it is likely failing. You can check on that by reading standard error using the getErrorStream() method of Process.

danben
  • 80,905
  • 18
  • 123
  • 145
  • I am just using ls for testing .. I want to run the java file 'NewFile' using this method – student1234 Jun 16 '13 at 16:08
  • The full path of the file is /Users/apple/Documents/Documents/workspace/UserTesting/src/NewFile.java – student1234 Jun 16 '13 at 16:08
  • Did you see the change I suggested? The same way you can pass the full (rather than relative) path to `ls`, you can also pass the full path to exec. Edited my answer to provide the code. – danben Jun 16 '13 at 16:11
  • Please check several trials updated in my question @danben – student1234 Jun 16 '13 at 16:16
  • I mean, now you're just pasting unrelated errors into your question. I answered what you originally asked about. – danben Jun 16 '13 at 16:24
  • you specifically wrote this 'java /Users/apple/Documents/Documents/workspace/UserTesting/NewFile' which I tried and showed you that it is not working .. that's why I posted this\ – student1234 Jun 16 '13 at 16:28
  • Sorry, SO isn't tech support. Read the error message. – danben Jun 16 '13 at 16:45