I am developing two Java applications in Netbeans, and I need to run the second app in the first one. I do this using the following code:
ProcessBuilder pb = new ProcessBuilder("java", "Second App Main Class Name");
pb.start();
The problem is that the second app has a huge number of dependencies which I added as references in Netbeans. But when I run it using above code, Java obviously finds non of them.
Since my project uses lots of Libraries, can I handle all the Classpath automatically somehow?
Well, the problem here is that you don’t really have a classpath configured in the environment for your second Java application, so even though you can invoke the main class itself (as you said,) none of the other dependencies are found.
I’d also guess that even if you use something like Maven or Ant, if you are building two separate Java applications with separate classpaths (but one knows about the other’s classpath, then you could do something like this in order to tell the ProcessBuilder how to find the right JAR and Class files for your second application:
But I question why you are launching the app from NetBeans – assumedly you will need to eventually launch both apps from the command line. If you can’t launch the apps from the command-line, then you won’t be able to launch them from Java either. ProcessBuilder just behaves as a command-line launcher for other programs on the system.