I have a jar file that is giving errors that have led to this question. How do I know if a jar is self contained or if it is loading external resources?
The jar references a license file (*.lic
) in the main constructor. Shouldn't this be included in the final jar build?
Here is the main class:
public static void main(String[] args) {
checkLicense();
}
private static void checkLicense() {
InputStream licenseStream = null;
try {
licenseStream = Main.class.getClassLoader().getResourceAsStream("Vendor.lic");
if (licenseStream == null) {
throw new NullPointerException();
}
} catch (Exception e) {
System.err.println("Missing Vendor license resource");
System.exit(1);
}
try {
License license = new License();
license.setLicense(licenseStream);
} catch (Exception e) {
System.err.println("Invalid Vendor license");
System.exit(1);
}
If the Vendor.lic
is not there then there is an error, "Missing Vendor license resource".
If the Vendor.lic
is there but the classes folder is not there I get another error for "java.lang.ClassNotFoundException".
I see two other .jar files in the same directory.
Can the license be included? Does the jar need other files to work correctly? It seems like it won't work if run independently outside of the folder structure it is in that looks something like this:
/myproject.jar
/classes/generatedsources
/classes/com/mydomain/myproject/Main.class
I create the jar using mvn clean package
.
FYI I'd like it to be self contained.
There is a POM.XML and that lists two entries in the dependencies
node. There is also a copy dependencies
in the plugin\executions
node.
Jar Manifest
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.3.0
Build-Jdk-Spec: 19
Class-Path: vendor-1.0.0-jdk16.jar helper-library-1.0.0.jar
Specification-Title: myproject
Specification-Version: 1.0
Implementation-Title: myproject
Implementation-Version: 1.0.0
Main-Class: com.domain.project.Main
The pom.xml:
Original:
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
Adjusted to relieve the problems errors:
<properties>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
Problems inspector prior to changing value:
More of the pom.xml:
<groupId>com.myproject</groupId>
<artifactId>myproject</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.mydomain.myproject.Main</mainClass>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
<dependencies>
<dependency>
<groupId>com.3rdPartyVendorDomain</groupId>
<artifactId>3rdPartyVendorDomain-library</artifactId>
<version>10</version>
<classifier>jdk16</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.2</version>
</dependency>
</dependencies>
The Jar is run from the command line using:
java -jar myproject.jar --parameter1 value --parameter2 value
My Java version:
> java --version
java 12.0.1 2019-04-16
Java(TM) SE Runtime Environment (build 12.0.1+12)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)