2

I want to execute another Java program in my program. I have taken reference from here. For testing I have pasted same code as accepted answer shows.I have passed a simple HelloWorld program. Program compiles perfectly but gives Main class not found error.

Here is my code: Server.java

public static void main(String args[]) {
    try {
        runProcess("javac D:\\HelloWorld.java");
        runProcess("java D:\\HelloWorld");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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);
    }
}

private static void runProcess(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    printLines(command + " stdout:", pro.getInputStream());
    printLines(command + " stderr:", pro.getErrorStream());
    pro.waitFor();
    System.out.println(command + " exitValue() " + pro.exitValue());
}

HelloWorld.java:

`public static void main(String args[]){
    System.out.println("Hello World!");
}`

Output: exitValue() 0 for javac stderr: Error: Could not find or load main class D:\HelloWorld exitValue() 1 for java

Compiling and running same program on CMD or IDE gives perfect output.

6
  • Could you show your code? Commented Dec 2, 2015 at 14:36
  • 1
    Is HelloWorld in a package? Also, have you tried running the "java D:\\HelloWorld" command yourself? Commented Dec 2, 2015 at 14:43
  • do you have the .class file in the alleged path? d:\HelloWorld Commented Dec 2, 2015 at 14:43
  • 1
    Your class (run with the java ___ command) is not going to be called D:\HelloWorld. It will be called HelloWorld or packagename/HelloWorld. Commented Dec 2, 2015 at 14:44
  • and not just that, D:\ won't be on the classpath unless it's explicitly added so it won't ever find the class if it's there. Commented Dec 2, 2015 at 14:57

2 Answers 2

1

You want to start main from HelloWorld class? I think, in that case you should run program something like this:

java -cp 'D:\' HelloWorld

So, you need to specify ClassPath - 'D:\' and entry class name from classpath - HelloWorld.

Sign up to request clarification or add additional context in comments.

Comments

-1

Why try to do things the hard way? Use the inline compiler API and then simply execute the main() method on your new class after loading the class itself into your root classloader.

1 Comment

May be ton of reasons. From unwillingness to load classes to the same classloader (or making some tricky stuff with classloaders because, for example, external code uses dependencies with another versions) to unstable external program, that you don't want to execute inside your main process to don't get OOM for example. Very specific question with valid field, no need to rephrase it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.