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?

asked May 13, 2012 at 2:19
6
  • What package is class Program in? At params.add("Program"); you need to provide the fully-qualified class name. Commented May 13, 2012 at 2:23
  • none of the classes have package. Anyway put both on package build.test so changed params.add("build.test.Program");. Still same error Commented May 13, 2012 at 2:26
  • Have you tried outputting the value of pathToJar? Is it what you expect? Commented May 13, 2012 at 2:29
  • 1
    Edit your post and include a copy/paste of the COMPLETE stacktrace Commented May 13, 2012 at 2:33
  • 1
    Try params.add(System.getProperty("java.class.path").concat(";").concat(pathToJar)); to replace params.add(pathToJar). Commented May 13, 2012 at 2:53

2 Answers 2

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))‌​;.

answered May 13, 2012 at 3:16
Sign up to request clarification or add additional context in comments.

Comments

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.

answered May 13, 2012 at 2:26

2 Comments

printed jarpath, its fine. Also tested with package, same error.
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.