ukor
ukor

Reputation: 80

How can I specify a classpath for executables created with javapackager?

The javapackager command in Java 8 is able to create standalone executables. The examples on the web usually only use a single jar file, but I have several jar files that need to be bundled into the application. The documentation states that it is possible to tell the bundler the classpath sending classPath=path through the -B option. However, in the resulting <applicationname>.cfg file inside the packaged application there is a line that says

app.classpath=

which is not affected at all by the classPath argument, but I have to manually edit it to include all the jars used by the app to make it work.

All this is on macOS, I couldn't try it on another platform yet.

Any insight greatly appreciated – a working example using more than one jar, for example.

Upvotes: 3

Views: 977

Answers (2)

user274595
user274595

Reputation: 486

The documentation is slightly flawed:

-Bclasspath=

with small p instead of capital P works (Tested on Windows and MacOS).

Upvotes: 2

GabeV
GabeV

Reputation: 1046

I have this problem and found an alternative solution that does not require modifying the cfg file's class path. My approach uses Maven but it can be done with Ant or scripting; just requires more work. The idea is to create a single jar file that has all of the contents of your main jar and all the dependent jars. This can be done with a single Maven plugin like so:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2-beta-4</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.yourapp.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I set the execution phase to prepare-package so that it runs before the app bundling, but you can change that as needed. The end result is a single jar named <appName>-<version>-jar-with-dependencies.jar which contains the extracted contents of all the dependent jars. You can test it using java -jar <jarName> before using it with javapackager.

Upvotes: 1

Related Questions