This question already has an answer here:
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 150Apples-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
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
.
来源:https://stackoverflow.com/questions/17135048/error-when-trying-to-execute-a-command-in-java