Java Main file not found - java

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

Related

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.

Cannot find or load main class from .jar file

So I wrote a project in Eclipse and when I click on the green run button it works fine. When I export it into a jar and I double click on it a quick command prompt appears saying "cannot find or load main class...". I tried everything. I tried renaming registry keys, reinstalling java, changing environment variables and of course turning it on and off again. I read all the other threads on stack and tried everything they suggested. Here is my Manifest file:
Manifest-Version: 1.0
Class-Path: .
Main-Class: main.Main
My Main class is in a package called main.
I'd like to suggest to change the package name into something other than main.
don't forget to make a run configuration, which specifies the name of your main class.
package all the related other jar files into the same package.
run the executable.jar as java -jar YourExecutable.Jar
And if it does not solve your problem, just see my MANIFEST.MF file in my Executable jar file. Maybe it can give you some clue.
Manifest-Version: 1.0
Rsrc-Class-Path: ./ concurrentlinkedhashmap-lru-1.2.jar slf4j-api-1.7.
5.jar org.simpleframework.jar openflowj-0.3.7-SNAPSHOT.jar mongo-java
-driver-2.11.2.jar netty-3.9.0.Final.jar guava-15.0.jar org.restlet.e
xt.jackson.jar logback-classic-1.0.13.jar joda-time-2.2.jar org.codeh
aus.jackson.core.jar org.codehaus.jackson.mapper.jar openflowj-0.3.7-
SNAPSHOT-sources.jar org.restlet.jar logback-core-1.0.13.jar org.rest
let.ext.slf4j.jar org.restlet.ext.simple.jar openflowj-0.3.7-SNAPSHOT
-javadoc.jar
Class-Path: .
Rsrc-Main-Class: etri.sdn.controller.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

NoClassDefFoundError thrown even when required class is present in classpath

Trying to run jar program and getting class not found error.
As shown in the figure, I have packaged all my dependent jar under lib folder and packaged it inside jar file.
Contents of lib directory:
In my manifest.mf file i have
Manifest-Version: 1.0
Class-Path: .\lib\arapi63.jar;.\lib\spring.jar;.\lib\commons-logging.j
ar;.\lib\log4j.jar;..\conf;.\lib\ojdbc14.jar
Created-By: 1.5.0_12 (Sun Microsystems Inc.)
Main-Class: RemedyRecord
when i try to run this jar using
java -jar remedyDSTX.jar
Exception in thread "main" java.lang.NoClassDefFoundError:
com/remedy/arsys/api/ARException
No ARException class is present in arapi63.jar which is present in my classpath and so am not sure why is that not referenced here...any thoughts?
Update:
Manifest-Version: 1.0
Class-Path: lib/arapi63.jar;lib/spring.jar;lib/commons-logging.jar;lib
/log4j.jar;lib/ojdbc14.jar
Created-By: 1.5.0_12 (Sun Microsystems Inc.)
Main-Class: RemedyRecord
I updated my manifest as per Jon's comment below but still am getting same error message.
I believe the problem is the format of your Class-Path entry.
As per the tutorial:
You specify classes to include in the Class-Path header field in the manifest file of an applet or application. The Class-Path header takes the following form:
Class-Path: jar1-name jar2-name directory-name/jar3-name
Note that this is space-separated, and uses a forward-slash between the directory name and the jar file name.
So try a Class-Path entry of:
Class-Path: lib/arapi63.jar lib/spring.jar lib/commons-logging.jar lib/log4j.jar lib/ojdbc14.jar
Note that I've removed the ../conf part - I don't believe you can add a directory to the classpath within a jar file manifest. (I've just tried, and it didn't work.)

while creating JAR from eclipse?How to set class path in manifest file ,

I am done trying with all the solutions provided. Please help with this:
I am trying to create an executable jar for my project with manifest file. But I keep on get the error no class found.
My manifest file looks like below and it is in project level. I create a jar from eclipse through export.
Manifest-Version: 1.0
Main-Class: com.tn.gov.runParser.RunParser
Class-Path: commons-logging-1.1.1.jar s.jar t.jar 3.jar j.jar o.jar s.jar util.jar xml.jar xml.jar
When you export your project into a JAR, on the prefrences/config page, under "Launch configuration" would be all the possible Classes to put in Main-Class: Also, you could edit MANIFEST.MF on the Main-Class: line manually.

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