How should my Java JAR main class path look like? - java

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.

Related

fail to execute jar file in linux

I had developed a standalone program which don't have any GUI, it's only use in the linux machine(without any GUI). So, i converted my java source code into jar file using the following command
jar cfm hardcoded.jar manifest.txt hardcoded.class
The jar file was successfully created but when i try to execute the jar file in the terminal,i get this error
no main manifest attribute, in hardcoded.jar
Some of the information said that the problem due to the manifest file but i can't figure out where is the root cause because i am very new to java on linux,Some people said that the package also need to include but where can i find my package name?.My manifest file is showed as the following
Manifest-Version: 1.0
Class-Path: ./ commons-logging-1.1.2.jar httpclient-4.5.2.jar htt
pcore-4.4.1.jar java-json.jar java-rt-jar-stubs-1.5.0.jar javax-ssl-1
_1.jar joda-time-2.2.jar
Class-Path: .
Main-Class: hardcoded
As you can see my manifest file, i had some others external library, i know eclipse have the build in function to solve this issue, how i need to solve it in linux environment ?

Creating Executable jar from CMD prompt

I have following class files,jar and manifest in a directory
Dot.class
Bridge.class
jsch.jar
MANIFEST.MD
Manifest file contains following two lines
Manifest-Version: 1.0
Main-Class:Dot
command used for creating jar file
jar cfm dot.jar MANIFEST.MD *
While executing generated jar it throws error saying no main manifest attribute
While seeing inside the generated jar META-INF folder contains auto generated manifest file, it doesn't have content for my main class.
I couldn't find successful steps ,Please correct me.
Had the same issue a few days ago and couldn't resolve it with the manifest file so I put the main class as a build parameter like this:
jar cfe Main.jar MainClass *.class
Add a space after ':' as in
Main-Class: Dot
Add a new line after the last line, in your case after Main-Class entry:
Manifest-Version: 1.0
Main-Class: Dot
The reason for 2. ist documented in https://docs.oracle.com/javase/tutorial/deployment/jar/modman.html in detail.
I tried below command and it works with the jar produced which i post.
java -cp "jsch.jar;." Dot

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.

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 Main file not found

I'm trying to create a jar with the jar tool.
Using the following command
jar.exe cmfv manifest.txt lol.jar Main.class
This generates a jar with the following manifest:
Manifest-Version: 1.0
Created-By: 1.7.0_03 (Oracle Corporation)
Main-Class: Main
When I run the jar from command line (java -jar lol.jar) it runs fine. But when I double click the jar in my folder it gives an error: "Could not find the main class: Main.Program will exit."
What could be causing this?
After trying some stuff out the Manifest currently looks like this:
Manifest-Version: 1.0
Class-Path: .
Created-By: 1.7.0_03 (Oracle Corporation)
Main-Class: code.Main
The Main class has a package declaration added. Inside the jar the 'code' folder/package is added. Still have the same error though.
You should put your Main class into a package, and adjust your manifest correspondingly. That should solve the problem
You need to put the Main class in your classpath. When you run it from the command line, your current directory is auto-added to your classpath, this is why it works from there. When you double click it, you are not specifying the path to the jar as in the classpath. There are ways to add the class to your classpath in your manifest.
Below is an example of one of my jars. lib is a folder within the jar, com/sample/CommandLineClient.class is my main class.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.5.0_12-b04 (Sun Microsystems Inc.)
Class-Path: lib/args4j-2.0.19.jar lib/axis.jar lib/axis-ant.jar lib/commons-discovery-0.2.jar lib/commons-logging-1.0.4.jar lib/jaxrpc.jar lib/log4j-1.2.8.jar lib/saaj.jar lib/wsdl4j-1.5.1.jar
Main-Class: com.sample.CommandLineClientSysIn
What is the file association with the .jar extensions?
Double click the jar file should make the jar run woth the javaw command.
Try making jar files to be executed by default with javaw.
Also take a look at this: http://www.wikihow.com/Run-a-.Jar-Java-File

Categories