Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 2 classes Tester.java and Main.java, I want to run Main.java from Tester.java .. I got this code from a question posted by other user and this is supposed to be the working code but it's not printing anything

Test.java:

package bachelor;
import java.io.BufferedReader; 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import bachelor.actions.SampleAction;

import java.io.File;
import java.lang.Character.UnicodeBlock;
import java.math.BigInteger;
import java.util.Hashtable;


public class Tester {


     private static void printLines(String name, InputStream ins) throws Exception {
         String line = null;
         BufferedReader in = new BufferedReader(new InputStreamReader(ins));
         while ((line = in.readLine()) != null) {
             System.out.println(name + " " + line);
         }
       }



    public static void main(String[] args){


        try {
            Process processCompile = Runtime.getRuntime().exec("javac Main.java");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Process processRun = null;
        try {
            processRun = Runtime.getRuntime().exec("java Main");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            printLines(" stdout:", processRun.getInputStream());
            printLines(" stderr:", processRun.getErrorStream());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }}

Main.java

public class Main {

    public static void main (String args[]){
        System.out.println("DONE SUCCESSFULLY");
    }


}

This is the error that appears although I put the 2 classes in the same package

javac Main.java stderr: javac: file not found: Main.java
javac Main.java stderr: Usage: javac <options> <source files>
javac Main.java stderr: use -help for a list of possible options
javac Main.java exitValue() 2
java Main stderr: Exception in thread "main" java.lang.NoClassDefFoundError: Main
java Main stderr: Caused by: java.lang.ClassNotFoundException: Main
java Main stderr:   at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
java Main stderr:   at java.security.AccessController.doPrivileged(Native Method)
java Main stderr:   at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
java Main stderr:   at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
java Main stderr:   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
java Main stderr:   at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
java Main exitValue() 1
share|improve this question
did u compiled both??? – Senthil Prabhu Apr 16 at 10:53
make sure to include path of the inner .java file relative to the main program.. – ay89 Apr 16 at 10:55
2  
Use ProcessBuilder instead of Runtime.getRuntime().exec. Make sure you set the working to the top level of the package of the Main class. Make sure you also include the package path if any. Of course, you could also take a look JavaCompiler instead – MadProgrammer Apr 16 at 10:55
"I got this code from a question posted by other user" It is rubbish. The handling of the Process is amateurish and fragile. Ignore it and find a better example. – Andrew Thompson Apr 16 at 11:01
1  
I cannot quite understand, I run the Tester.java class and what I need is the printing DONE "SUCCESSFULLY" – Student Apr 16 at 11:08
show 1 more comment

1 Answer

As posted Tester.java is inside a directory/package "bachelor" while Main.java is not. This means there is a directory dirx that contains Main.java and a subdirectory bachelor which itself contains Tester.java. I am assuming that "java" and "javac" commands are in your PATH. Then, modify Tester.java as follows:

processCompile = Runtime.getRuntime().exec(new String[]{"javac", "Main.java"}, null, new File("PATH-TO-dirx"));
processCompile.waitFor();

and

processRun = Runtime.getRuntime().exec(new String[]{"java", "Main"}, null, new File("PATH-TO-dirx"));
processRun.waitFor();

And you will have to catch an InterruptedException.

Now open a console and change into dirx. Issue the command

javac bachelor/Tester.java

then

java bachelor.Tester

Tested and running on Linux.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.