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.

Community
  • 1
  • 1
Sarjit Delivala
  • 567
  • 2
  • 13
  • 25
  • Could you show your code? – hdost Dec 02 '15 at 14:36
  • 1
    Is `HelloWorld` in a package? Also, have you tried running the "java D:\\HelloWorld" command yourself? – MrHug Dec 02 '15 at 14:43
  • do you have the `.class` file in the alleged path? `d:\HelloWorld` – Yazan Dec 02 '15 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`. – khelwood Dec 02 '15 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. – jwenting Dec 02 '15 at 14:57
  • As mentioned above, you probably want to cd to the correct folder where the HelloWorld.class file was created before trying to run it. Or maybe you could simply use `runProcess("d: && java HelloWorld");` – uzilan Dec 02 '15 at 14:59

2 Answers2

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.

Dmitry Spikhalskiy
  • 5,379
  • 1
  • 26
  • 40
-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.

jwenting
  • 5,505
  • 2
  • 25
  • 30
  • 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. – Dmitry Spikhalskiy Dec 02 '15 at 15:11