Hi,

I have a project which calls an internal jar included in it´s classpath. At a certain moment my code makes this call:

Runtime.getRuntime().exec("java -jar ./jars/myJar.jar");

It works when running from Netbeans, but when I make "clean and build" and then execute the new jar ( outside of Netbeans ), doesn´t work the execution of myJar.jar (I mean: the main project works, but myJar execution doesn´t). I guess the path is modifyed, because the internal jar is included in the main jar.

Is the relative path changed? How could I reach my internal jar file path when executing the entire project made a jar file?

Thanks!

Recommended Answers

All 5 Replies

calls an internal jar

What is an "internal jar"?

the internal jar is included in the main jar.

You can not use a jar file that is inside another jar file this way. The JVM doesn't look inside jar files for other jar files.

Given this path,

./jars/myJar.jar"

Where is the jar file located relative to the folder where the program executing is located?

To put a second jar file on the classpath with the one in the java -jar command, use the Class-path: field in the manifest file in the first jar file.

> At a certain moment my code makes this call:

There is no need to spawn a separate process for this; just make sure that the JAR is in your classpath and invoke the `main` method of the class which has that method. E.g.

import your.jar.pkg.MainClass;

public class Test {
  public static void main(final String[] args) {
    MainClass.main(null);
    // The above would same as your Runtime call given that the
    // main class for that JAR file is MainClass.
  }
}

Hi thank you both for responsing.

I solved the problem by asking the path at runtime:

String path = System.getProperty("user.dir");

After this, I add "lib" name folder and there I put the jar I´m calling to. I have to say that I´m working with Netbeans, so when building it generates a jar file and a lib directory with all jars included: I´ve just put my extra jar in it.

The jar file I´m calling to, is a built project that I can´t run just by calling it´s main method: it requires a dedicated thread for it´s execution (or I don´t know how to prepare that project´s enviroment).

In Windows7 my solution worked out. I have to check if in XP, Linux or MacOS gets the path as well.

Thank you very much, bye!

public static void main(final String[] args) {
    MainClass.main(null);

A null could be unexpected. I think an empty array would be better:

public static void main(final String[] args) {
    MainClass.main(new String[]{});

A null could be unexpected. I think an empty array would be better:

If you look at the Java process invocation posted in the first post;

java -jar ./jars/myJar.jar

No command line arguments passed so no fear of running into NPE.

But then again, that was just an example to be fixed by OP as he sees fits. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.