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

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.

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!

Compiling from command line doesn't use libraries

I'm working on a project where I need to compile .java files 'automaticly'(So not using Eclipse)
And now I have some trouble with the libraries that are required by the sources that I compile.
The problem is that the compiled code extends a class which is inside a library.
I've added classpaths, and made sure the libraries where actually in the correct directory.
But it doesn't seem to be able to run.
I get an error which says that the class can not be found or loaded.
I modified the test source so that it doesn't extend. And then it does work.
How do I make the compiled class be able to actually use the classes from libraries in the class path?
I use this to compile:
javac -d /bin -cp kit.jar src/com/indieveloper/kittest/*.java
The class files compile with no error, and also in the correct package hierarchy
This is the manifest:
Manifest-Version: 1.0
Class-Path: .;lib/kit.jar
Created-By: Me
Main-Class: com.indieveloper.kittest.Main
The folder lib with the jar is in the same folder where the final packed .jar also ends up
Which I build using this code:
jar cvfm test.jar manifest.txt -C bin/ .
The inside of the final jar seems okay to me:
META-INF/
META-INF/MANIFEST.MF
com/
com/indieveloper/
com/indieveloper/kittest/
com/indieveloper/kittest/Main.class
The manifest is still correct(Nothing changed the manifest during 'jarring')
I also tried putting the lib folder with the jar inside the final jar, but that was a hopeless attempt..
The exact error:
java -jar test.jar
Error: Could not find or load main class com.indieveloper.kittest.Main
Does anyone know I can do to make it work?

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.

cannot include external jar in my executable jar

I am trying to convert my java application to an executable jar.
I have been able to package it into a jar with all my class files, but still am not able to include the jmf.jar file.
This is my directory structure,
Main dir
src/ //A dir
--a.java
--b.java
jmf.jar
Manifest.txt
Content of my Manifest.txt file,
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Main-Class: src/a
Class-Path: .;jmf.jar //also tried ./jmf.jar
By double clicking the .jar file, I am able to run the main function, but the second function uses the jmf.jar throws exception when I tried running the jar file in debug mode, that javax.media not found. This class is in the jmf.jar file, which makes me conclude the jmf.jar file is not being included in the class path or so. So what am I doing wrong? How do I make this executable file?
The command I used to convert it to jar was,
jar cvfm myJar.jar Manifest.txt src\*.class jmf.jar
The jar files referred to by the Class-Path entry in the manifest must not be inside the jar, but outside of it, as explained in the jar tutorial.
If you are using JMF you’ll need to include jmf.jar and jmf.properties in the same directory as the executable jar

How should my Java JAR main class path look like?

So I have Test.jar. It's directories look like:
META-INF/MANIFEST.MF
Test/src/test/Test.java
/MainFrame.java
/MainPanel.java
/image.png
And my mainfest file looks like:
Manifest-Version: 1.0
Created-By: 1.7.0_13 (Oracle Corporation)
Main-Class: test.Test
When launching from command line (java -jar Test.jar) i get such error: could not find or load main class test.Test. How to solve it? I know it's problem with Main-Class line in manifest but I dont know how should path look like..
thats because your jar apparently contains java source files and not compiled java class files.
your jar layout should be
META-INF/MANIFEST.MF
/test/Test.class
/MainFrame.class
/MainPanel.class
/image.png
your manifest is fine. you should compile your source code files (*.java) to produce *.class files and package those into your jar.

Categories