1

I'm having problems using ProcessBuilder to run a class in my project. My code:

public class Main {
    public static void main(String[] args) {
        try {
            String pathToJar = Main.class.getProtectionDomain().getCodeSource()
                    .getLocation().toURI().getPath();
            ArrayList<String> params = new ArrayList<String>();    
            params.add("javaw");
            params.add("-classpath");
            params.add(pathToJar);
            params.add("Program");
            ProcessBuilder pb = new ProcessBuilder(params);
            Process process = pb.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Program is in same project (same bin folder) and works fine if ran directly but this way I get the error "Could not find the main class: Program". Where is the error in my code?

Thanks in advance.

[EDIT] I came to the conclution that is some code on my Program class giving error. Basicly only runs with "clean" main. At eclipse, Program class is importing some libraries that are inside a jar file. Don't I need to reference it in ProcessBuilder? If so, how?

Ciro
  • 662
  • 7
  • 19
  • What ***package*** is class `Program` in? At `params.add("Program");` you need to provide the fully-qualified class name. – Jim Garrison May 13 '12 at 02:23
  • none of the classes have package. Anyway put both on package build.test so changed `params.add("build.test.Program");`. Still same error – Ciro May 13 '12 at 02:26
  • Have you tried outputting the value of `pathToJar`? Is it what you expect? – Jim Garrison May 13 '12 at 02:29
  • yes its ok. Also, if I go to windows cmd, directly to the directory and execute `javaw build.test.Main` works but `javaw build.test.Program` doesn't. – Ciro May 13 '12 at 02:32
  • 1
    Edit your post and include a copy/paste of the COMPLETE stacktrace – Jim Garrison May 13 '12 at 02:33
  • 1
    Try `params.add(System.getProperty("java.class.path").concat(";").concat(pathToJar));` to replace `params.add(pathToJar)`. – Krrose27 May 13 '12 at 02:53

2 Answers2

2

In response to your edit:

You can add the current path by switching params.add(pathToJar); with params.add(System.getProperty("java.class.path").concat(";").concat(pathToJar))‌​;.

Krrose27
  • 1,026
  • 8
  • 13
1

Where is the error in my code?

(You are launching the javaw executable, so that is not the problem. It is also not that your entry point method's signature is incorrect, because that would have given a different diagnostic.)

The problem is either that the class name is incorrect (e.g. if should be "come.pkg.Program"), or the pathname for the JAR file is incorrect.


Assuming that you have eliminated the possibility that the class name is incorrect, my guess is that you are trying to use a relative pathname for the JAR file, but there is some confusion over what the current directory is; i.e. the directory in which the pathname needs to be resolved. Try using an absolute pathname in the classpath parameter.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • printed jarpath, its fine. Also tested with package, same error. – Ciro May 13 '12 at 02:29
  • I might be wrong about the entry point. If none of the above helps, show us the signature of the `Program` class and its `main` method. – Stephen C May 13 '12 at 02:38