Create a jar with a custom manifest with several libraries in the classpath

I am trying to create a jar from my eclipse and in order to use external .jars, I use this manifest with several .jar in the classpath:

Manifest-Version: 1.0 Sealed: true Main-Class: src.BatchTester Class-Path: . P:/Tools/xstream/1.4.2/lib/kxml2-2.3.0.jar P:/Tools/xstream/1.4.2/lib/xstream-1.4.2.jar P:/Tools/StringTemplate/4.0.5/lib/antlr-3.3-complete.jar P:/Tools/StringTemplate/4.0.5/lib/ST-4.0.5.jar P:/Tools/Jdbc/lib/sqljdbc4.jar 

Obviously, if I do not put the libraries in the classpath, the following message appears:

 java.lang.NoClassDefFoundError: com/thoughtworks/xstream/XStream 

But when I put them in the classpath, the error will change to:

 java.lang.NoClassDefFoundError: src/BatchTester 

It seems like he cannot find my main class. I tried to use several features in the classpath, such as adding or removing . into the classpath, but can't make it work.

Any idea how I can solve this problem?

Thank you for your time and efforts,


PS: After creating the .jar, the class path inside the manifest looks like this:

 Class-Path: . P:/Tools/xstream/1.4.2/lib/kxml2-2.3.0.jar P:/Tools/xstr eam/1.4.2/lib/xstream-1.4.2.jar P:/Tools/StringTemplate/4.0.5/lib/ant lr-3.3-complete.jar P:/Tools/StringTemplate/4.0.5/lib/ST-4.0.5.jar P: /Tools/Jdbc/lib/sqljdbc4.jar 

with new lines and spaces, but even after changing it to the "right" format, I had the same problems.

PS2: I know that with some plugins, such as Fat-Jar, you can make it work, but I do not want to insert more data than is necessary in my .jar

+4
source share
2 answers

Finally, I copied all the libraries to the / lib folder and added them to the .jar for the purpose of ant, since everything seems to be fine with the IT guys (because it is a small application).

Here is ant (in case this is useful to someone):

 <?xml version="1.0" encoding="UTF-8"?> <project name="BatchTester" default="compile" basedir="."> <property name="external" value="lib/external-libs.jar"/> <target name="compile"> <javac srcdir="jav" source="1.6" /> <echo>Creating jar File</echo> <!--create a new .jar with all the external jars in /lib--> <jar jarfile="${external}"> <zipgroupfileset dir="lib/"> <include name="**/*.jar"/> </zipgroupfileset> </jar> <!--<sleep seconds="1"/>--> <!--create .jar file--> <jar jarfile="BatchTester.jar" index="true" filesetmanifest="mergewithoutmain"> <fileset dir="."> <include name="**/jav/**/*.class"/> <exclude name="**/jav/**/*.java"/> </fileset> <zipfileset src="${external}"> <exclude name="META-INF/*.SF"/> </zipfileset> <manifest> <attribute name="Main-Class" value="jav.BatchTester"/> </manifest> </jar> <!--delete previously created extern .jar--> <delete file="${external}"/> </target> </project> 
+6
source

Sorry if my questions seem obvious to you.


* Run the command *

To exclude any doubts, haven't you tried to launch your bank with such a team?

 java -jar myJar.jar -cp ./lib 

If you use the classpath option, you probably haven't;). The option --classpath (or -cp) and -jar cannot be used together.

Prefer using a relative path, for example. / lib instead of P: / Tools / ... But, in any case, this will not solve your problem.


* Package location *

As brimborium said, what kind of package are you? src sounds very strange. We suspect a mistake in this.

In the BatchTester class, what did you write for the package directive? Nothing (like the default package, which is not recommended?)?

You start with the class (get rid of comments)

 public class BatchTester { 

In this case, for sure, src should not be mentioned.

Here is an example manifest that works for me.

 Manifest-Version: 1.0 Archiver-Version: Plexus Archiver Created-By: Apache Maven Built-By: jrRevy Build-Jdk: 1.6.0_31 Main-Class: com.sopragroup.training.dojo1.MainSwingApp Class-Path: dojo1-0.5.0-SNAPSHOT-lib/spring-core-3.1.1.RELEASE.jar doj o1-0.5.0-SNAPSHOT-lib/spring-asm-3.1.1.RELEASE.jar [blablabla] 

with the following execution structure

 / | + --dojo1-0.5.0-SNAPSHOT.jar | + --dojo1-0.5.0-SNAPSHOT-lib/ | + --spring-core-3.1.1.RELEASE.jar 

Obviously, I am using maven to build my application, but the main idea is in it.

+2
source

Source: https://habr.com/ru/post/1416536/


All Articles