1

The tester program that I wanted execute takes in a single argument -- a filename -- and makes a copy of the file with the line "This is a modified version." at the top of the new file. When I tested this program alone, it works and produces a new file.

Then I wrote the program to call the file:

public static void main(String[] args) {
    try {
        Process p = Runtime.getRuntime.exec("java Tester.java inputfilename.txt");
        p.waitFor();
        System.out.println("Done");
    } catch(Exception e) {
        System.out.println("Error");
        System.exit(0);
    }
} 

The program above printed out "Done" but it never made a modified version of the file I passed in. I then put some println()'s in the other program. When I run that program alone, it printed out those statements, but when I tried to call it from the program above, it did not. How do I fix this?

1
  • Jari told you why it did not work in general. But concerning the output: You explicitly have to read the output that is generated by the program that you start there. Also look at stackoverflow.com/a/21904185 Commented Mar 23, 2014 at 19:03

2 Answers 2

3

You have to compile .java file first and launch it later:

Compile (class containing the main method):

javac Tester.java

Launch:

java Tester inputfilename.txt
Sign up to request clarification or add additional context in comments.

6 Comments

I added (before the line:Process p = Runtime.getRuntime.exec("java Tester.java inputfilename.txt");) the line: Runtime.getRuntime().exec("javac Tester.java"); and then ran the program. It still didn't work. Is my syntax wrong?
Open cmd, run this stuff manually and tell me what errors you get.
The message I got: Exception in thread "main" java.lang.NoClassDefFoundError: Tester/java Caused by: java.lang.ClassNotFoundException: Tester.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)
Well, that a different question, check this stackoverflow.com/questions/6334148/… or just google for it.
.NoClassDefFoundError: Tester/java indicates you are still adding the .java extension when using the java command! Jari advised you around an hour before that, to remove it!
|
1
"java Tester.java inputfilename.txt"

Should be:

"java Tester inputfilename.txt"

But do yourself a favor and read (and implement) all the recommendations of When Runtime.exec() won't.

That might solve other problems. If not, it should provide more information as to the reason for failure.

Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces.

Comments

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.