Code runs in IDE but generated jar file doesn't

Answered

I have a project that I have struggled to compile due to java version problems and deprecated libraries (not my code!). I converted it to a Maven project and I can now get it to run in the IDE but I have yet to figure out how to create a jar file that runs. I have tried all sorts and ended up with a range of issues including "could not find or load main class main" and "Unable to initialize main class main".

I'm not a java programmer. I can cope with the language but really struggle with the environment and this particular issue has stumped me for over 2 weeks now. I'd be grateful for any assistance.

0
2 comments

Main points to make sure is that your jar must contain the manifest file with the main class specified and the classpath defined.

If you want to use IDE for creating jar file, see Create Jar from Modules Dialog about two options you have (see The way the JAR files from the module libraries are processed section).

If you want to use Maven for this, please check related forum threads, e.g. https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven

0
Avatar
Permanently deleted user

Andrey Dernov

Thanks. I have already tried those methods. For the Maven option my pom file has evolved to ...

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.vec</groupId>
<artifactId>manager</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.vec</groupId>
<artifactId>HLALibrary</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/HLALibrary.jar</systemPath>
</dependency>

<dependency>
<groupId>org.vec</groupId>
<artifactId>rti_driver</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/rti_driver.jar</systemPath>
</dependency>

<dependency>
<groupId>org.vec</groupId>
<artifactId>prticore</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/prticore.jar</systemPath>
</dependency>

<dependency>
<groupId>org.vec</groupId>
<artifactId>prti1516e</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/prti1516e.jar</systemPath>
</dependency>

<dependency>
<groupId>org.vec</groupId>
<artifactId>booster1516</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/booster1516.jar</systemPath>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.vec</groupId>-->
<!-- <artifactId>mssql-jdbc</artifactId>-->
<!-- <version>6.4.0</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${basedir}/lib/mssql-jdbc-6.4.0.jre8.jar</systemPath>-->
<!-- </dependency>-->


<!-- <dependency>-->
<!-- <groupId>commons-cli</groupId>-->
<!-- <artifactId>commons-cli</artifactId>-->
<!-- <version>1.4</version>-->
<!-- </dependency>-->

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>

<!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-cli</artifactId>-->
<!-- <version>1.0</version>-->
<!-- <scope>system</scope>-->
<!-- <systemPath>${basedir}/lib/commons-cli-1.4.jar</systemPath>-->
<!-- </dependency>-->

<dependency>
<groupId>com.oracle</groupId>
<artifactId>javaFX</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jfxrt.jar</systemPath>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>main</mainClass>
<classpathPrefix>java/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>manager</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
0

Please sign in to leave a comment.