Setting the Classpath in (Eclipse's) Jar File - java

I have a program.jar file which includes some external libraries. Additionally I wan't to start some classes with the program.jar.
The classes (TestKlass.class,...) are located in <path>/bin/data/test + sometimes there will be new classes added here.
How can I set this location for the classpath so I can use this command in the jar file:
cStart = Class.forName("data.test.TestKlass.class");
This is how my current MANIFEST.MF looks like:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ junit-4.10.jar selenium-java-2.20.0.jar WinRegistr
y-4.4.jar selenium-server.jar
Class-Path: .
Rsrc-Main-Class: data.Testworks
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
(Everything works fine within eclipse)

You can't reference external jars classes in a runnable jar file. Everything must be contained inside the jar.

Related

jar throwing NoClassDefFoundError

I've got a project that is structured liked so:
root/
-- lib/
---- commons-cli-1.2.jar
-- src/
---- my/package/name/*.java
-- bin/
---- my/package/name/*.class
-- .classpath
-- .project
-- manifest.mf
The *.class files in bin/ are made by Eclipse each build. My .classpath has the lib/ included and compiles just fine; it also runs as a "Java Application" just fine with my current stubs.
The issue comes about when I try to create a JAR and include the lib/ dependencies. From the command line I've been issuing:
jar cvfm prog.jar manifest.mf -C bin/ .
The program builds, and then when I try to run java -jar prog.jar, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/Option... (there is more, but I cant copy from the other machine)
My manifest.mf looks like:
Manifest-Version: 1.0
Main-Class: my.package.Main
Class-Path: ./lib/commons-cli-1.2.jar
Seems to me that the Class-Path isn't being included and the JAR cannot find the classes contained in the commons-cli-1.2.jar. I've isolated this to JAR creation, since I can run the compiled classes with no issue.
What am I doing wrong when creating the JAR and including the lib/*.jar?
I have had issues with specifying classpath in the manifest file before. If I were you, I would skip referencing the required libraries in the manifest file and instead include them directly inside your jar. Eclipse allows you to easily do this link
You probably assume that the build process should pull the commons JAR into the new that holds your classes. Wrong assumption.
The default Java approach is that you only include your classes in your jar. But instruct your users that they need to have the commons jar in their class path as well!

How exactly do I compile my Java into a JAR file?

So I'm trying to compile my game for my Object-Oriented Programming class into a jar file so it can be ran with java -jar javacoffeeadventure.jar.
My folder structure for a folder with java files removed looks like this:
audiomanager/
commandinterpreter/
gamemanager/
io/
logic/
META-INF/
player/
resources/
rooms/
main.class
Everything is packaged under javacoffeeadventure. For example, the main file is javacoffeeadventure.main. The META-INF folder contains one MANIFEST.MF file that I tried to edit and make the jar invoke main.class's main() method:
Manifest-Version: 1.0
Created-By: 1.8.0_60 (Oracle Corporation)
Main-Class: javacoffeeadventure.main
I know I use jar to compile into a jar file, but how would I use that command to create a jar file that is able to begin with javacoffeeadventure.main? Is my manifest wrong?
as a slight by-the-way, jar puns are funny to me if you guys have any. :)
In my experience, the following has worked, but if you utilize it, you may need to adapt your directory structure.
First, your folder structure needs to be of the form 'containing_folder.com.example.package', where your classes are in the 'package' folder. You need to then place your manifest.mf file in the uppermost directory ('folder'). The manifest file should be as follows:
Manifest-Version: 1.0
Main-Class: com/example/package/javacoffeeadventure
including a carriage return after the second line.
From the initial folder compile with the following command:
jar cmvf manifest.mf javacoffeeadventure.jar com/example/package/*.class
making sure that beforehand you've compiled the classes in your package (use *.java)
Hope this helps.

Java jar - no main manifest attribute

I'm aware that jar files should have a manifest.mf Main-Class: attribute in order to have an entry point and make the jar file runnable. Now, I have a jar file that I've built per below. The classes are all part of the burrito package. My MANIFEST.MF file looks like:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.1
Created-By: 1.7.0_51-b13 (Oracle Corporation)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: burrito.Main
, and when I try to build and execute via the following:
jar -cvf Burrito.jar Customer.class Main.class Server
.class Store.class MANIFEST.MF
*added manifest
adding: Customer.class(in = 2800) (out= 1424)(deflated 49%)
adding: Main.class(in = 1147) (out= 757)(deflated 34%)
adding: Server.class(in = 3954) (out= 2094)(deflated 47%)
adding: Store.class(in = 3950) (out= 2190)(deflated 44%)
adding: MANIFEST.MF(in = 203) (out= 158)(deflated 22%)*
I get:
Burrito.jar
*java -jar Burrito.jar
no main manifest attribute, in Burrito.jar*
I've tried various ways, also trying the -m switch (cvfm). I've attempted the following:
java -cp Burrito.jar burrito.Main
as well as
java -cp Burrito.jar Main
, which both tell me Error: Could not find or load main class
I've been poring over forums, and I can't seem to 'Google' my way to a solution here. Of course the Netbeans jar works, but I need to build my own. I've peeked at the Netbeans jar, and I see that I have two folders, burrito and META-INF. Of course, the manifest file is in the META-INF folder, and the burrito folder contains the class files. I'm not sure exactly to mimic this, and I would be happy to simply get this program to run.
Any pointers/hints?
You want something like this:
jar -cvfm Burrito.jar MANIFEST.MF burrito/Customer.class burrito/Main.class burrito/Server.class burrito/Store.class
The first argument after the options correspond to the f flag (the file to create), and the second argument corresponds to the m flag (the manifest file), and all the other arguments are the files to add to the JAR. You have to ensure the folder structure in the JAR matches the Java package, so if burrito.Main is the class, then you need burrito/Main.class in the JAR, and similarly for the other classes.

java.lang.NoClassDefFoundError - Generating Executable Jar

I have created my JAR on Windows 2000 having java version 1.5 which contains following directories/files:
manifest.txt
com
lib
lib contains all JARS which I want to make part of my JAR. com contains my class files and below is manfiest.txt file
Main-Class: com.as.qst.result.ResultTriggerSchedular
Class-Path: lib/axis.jar lib/c3p0-0.9.1.1.jar lib/commons-discovery-0.2.jar lib/commons-logging-1.0.4.jar lib/jaxrpc.jar lib/log4j-1.2.16.jar lib/medplus-hub-8.2-wsclients.jar lib/medplus-hub-13.1-jaxws-clients.jar lib/quartz-2.2.1.jar lib/quartz-jobs-2.2.1.jar lib/saaj.jar lib/slf4j-api-1.6.6.jar lib/slf4j-log4j12-1.6.6.jar lib/ wsdl4j-1.5.1.jar lib/xercesImpl.jar com\as\qst\result
I used following command to generate my JAR
jar cvfm test.jar manifest.txt com lib
It has successfully created a JAR file but when I try to run it with
java -jar test.jar
it does not execute and throws above exception. I used the same process for Windows 7 which has version 1.7 and it did work out even without class files path in manifest.txt com\as\qst\result. Is something more to do with class-path besides defining in manifest? and why is it working in Windows 7?
You do not need the class file path in your class path entry. So instead of adding com\as\qst\result to your class-path.
More over you must not package other jar files in your runnable jar.
Other required jars must be provided in the same folder as your jar file (may be in separate folder) and Add current directory "." (without quotes) to your class-path.
Hope this helps.
EDIT
Just found this Stackoverflow Link. This might give you more insight. Please read through it.

How to run a java file with a lot of jars dependencies in ubuntu

I have a java class which has almost 12 jar file dependencies and i am using ubuntu 12.10 . I need to know how to run this java application because every time i run it , it gives me errors as "symbols not found". I have all jar files in a folder called libs. and i have tried these commands but none of these gives me some succesful result.I have flights.java class in test directory and libs directory is inside test directory.Currently i am in test directory
javac -cp "/home/ubuntu/test/libs/*.jar" flights.java
javac -cp '/home/ubuntu/test/libs/*.jar' flights.java
if you have single class in your app called flights.java and all of your required jar are located at /home/ubuntu/test/libs/ then use this
javac -cp '.:/home/ubuntu/test/libs/*.jar' flights.java
and to run
java -cp '.:/home/ubuntu/test/libs/*.jar' flights
better to just pack dependency and app in to a single jar and make it launchable and runnable jar
12 jars is not a very large number. Why not just append all the jars on the classpath?
Alternatively, you can create another jar and specify all the jars in Class-Path variable in that jar's MANIFEST.MF and then add this single jar to your classpath.
EDIT:
Here is how I would do it. Create a MANIFEST.MF file with content similar to this:
Manifest-Version: 1.0
Archiver-Version: whatever
Created-By: whatever
Built-By: author-name
Build-Jdk: 1.6.0_34
Class-Path: jar1.jar jar2.jar jar3.jar
replace jar1.jar with the actual file names of the jar.
Then you can create a jar with command : jar cvf test.jar -m ./MANIFEST.MF .
Now when you are using it on classpath use it like java -jar xyz.jar class-name

Categories