Java JAR Couldn't find main class - java

64x Intel processor
Kubuntu 19.04 on Linux 5.0.0-27
OpenJDK 1.8.0_222
In the last hour I have set up my project folder, created scripts for automatic compiling and for back-ups etc...
Finally I got it working, I get the JAR file. The problem is that I can't run it. I know that this is very common problem, but I don't see any compiling errors, mistakes nor typos. I'm totally lost.
Even using java -jar TaskCalender.jar com.ydsoftware.taskcalender.EntryPoint and java -cp TaskCalender.jar com.ydsoftware.taskcalender.EntryPoint show the same error.
Error: Could not find or load main class com.ydsoftware.taskcalender.EntryPoint
My source file structure:
com
ydsoftware
taskcalender
EntryPoint.java (contains main method)
My JAR file structure:
META-INF
MANIFEST.MF
com
ydsoftware
taskcalender
EntryPoint.class (contains main method)
MANIFEST.MF contents (inside the JAR file):
Manifest-Version: 1.0
Created-By: 1.8.0_222 (Private Build)
Main-Class: com.ydsoftware.taskcalender.EntryPoint
Class-Path: lib/MX%20Engine.jar
/ blank /
/ blank /

Can you try below in CMD or through batch file as per your convenience
Generic:
java -cp your_jar_name.jar package_name.classname
as per your class:
java -cp TaskCalender.jar com.ydsoftware.taskcalender.EntryPoint

SOLVED
The problem was that Class-Path: lib/MX%20Engine.jar was missing because my computer had shut down unexpectably (because our power was cut off for some reason) and the changes to MANIFEST.MF were lost. Didn't even notice.
Still, it should say something along the lines of Class not found or unexcepted token instead of saying Could not find or load main class which made finding the problem harder.
Thank you to everyone who tried to help me!

Related

Java jar file "no main manifest attribute"

To start, I have looked at the similar questions posted about this topic. I have not found a solution.
I wish to distribute a small project I have been working on to friends and family. Trouble is I can't expect them all to be able to run .class files. Therefore I am trying to produce a .jar file that I can turn into an .exe file. I am however running into an issue. It complains about missing the main manifest attribute. After some googling, I resolved to try a little tutorial. I am following the tutorial (even downloading the little Hello.jar file), but still I get no main manifest attribute.
As I am running Windows 10, I am using PowerShell to run commands. This is the commands I've run:
PS C:\testJarFraNettet> javac Hello.java
PS C:\testJarFraNettet> jar cmf Hello.mf Hello.jar Hello.class Hello.java
PS C:\testJarFraNettet> java -jar Hello.jar
no main manifest attribute, in Hello.jar
PS C:\testJarFraNettet>
You'll notice I've included the .java file in the .jar as well, just to be sure.
Hello.mf contains:
Manifest-Version: 1.0
Main-Class: Hello
I investigated the .jar file that gets produced. Inside, among the files specified in the command, I find the expected .\META-INF\MANIFEST.MF file with the following content:
Manifest-Version: 1.0
Created-By: 17.0.1 (Eclipse Adoptium)
I've never touched Eclipse (I use VS Code), but I assume this is of no consequence.
Per another article I found online, I edited the file to instead contain what the tutorial tells me it should contain:
Manifest-Version: 1.0
Main-Class: Hello
Still no luck. I've decided against showing my own code, as it needlessly complicates troubleshooting since I get the same error anyways. I still have some hurdles left, such as how to add image files. The project is a GUI based game. But that is an issue for later, I suppose.
Thank you for your time!
jar cfe Hello.jar Hello Hello.class Hello.java
will do it for you. You need to point the -e parameter to the main class so that jar knows how to make it executable. This is easier than making your own manifest

Package structure and jar file with manifest

Let's say I have the following "project" structure.
*Root/
- manifest.txt
+ com/
+ mypackage/
+ example/
- MyClass.java
- MyClass.class
I know from the docs.oracle.com documentation that the manifest file must be written in the UTF-8 character set and that there must be a space after each colon, and a carriage return ('\r') or a new line ('\n') before save file.
/* ** MANIFEST.TXT ** */
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.mypackage.example.MyClass
/* ** END OF MANIFEST ** */
Creating an executable .jar file using cmd from the root directory.
CMD:
jar -cvfm out.jar manifest.txt com/mypackage/example/*.class
OUTPUT from CMD:
added manifest
adding: com/mypackage/example/MyClass.class(in = 648) (out= 445)(deflated 31%)
Now I'm trying to run the newly created jar file from the root directory:
java -jar out.jar
OUTPUT from CMD:
Error: Could not find or load main class com.mypackage.example.MyClass
I can't get to the goal despite the last problem. What am I doing wrong??? Thank You.
Here is a screenshot of the screen and cmd.:
As you described it, you will get a running jar, and therefore, what you described is not an accurate representation of what you did.
A few steps to check:
Did you paste the complete error message? For example, maybe the error message includes the note Caused by: java.lang.NoClassDefFoundError: MyClass (wrong name: com/mypackage/MyClass) or something along that vein. Then the problem is: Your MyClass.java needs to start with package com.mypackage.example;, it needs to contain class MyClass with that exact casing, and it needs to be called MyClass.java with that exact casing.
Did you actually do all the steps exactly as you laid out in your question? I did, and it works fine. This isn't one of those 'works on one machine and may not work on another' kind of scenarios, which is why I conclude you didn't. For example, if you include just MyClass.class and not com/mypackage/example/MyClass.class (i.e. you run the jar command from something that isn't the directory containing the com directory), you'd get this problem.
NB: 99% of folks building java jars use a build system to do so, such as maven or gradle.
Class-Path: com.mypackage.examples
Main-Class:My class
On Unix don't forget to set the .jar executable permission.
And alternate with no manifest.
Java -cp thejar.jar com.mypackage.examples
Don't forget the main class must have a
public static void main(String args[])
method to start the class.

Exporting jar from eclipse won't work

I built a Java project in eclipse. It runs fine from within eclipse. I've exported it as a jar before and it's worked fine. It's been a while since the last export, though. This time, when I exported and clicked on the jar, nothing happened.
Running it from the command line reveals the following error:
C:\>java -jar "C:\Users\Quinn\Desktop\think.jar"
Exception in thread "main" java.lang.IllegalAccessException: Class org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader can not access a member of class think.Main with modifiers "public static"
What does this error mean? Are there any reasons why this could happen? I haven't been able to find any mention of it online.
Thanks a lot.
EDIT:
when I unzip the jar, the contents looks like this:
the manifest (in META-INF) looks like this: (yes, it has an extra empty line)
Manifest-Version: 1.0
Rsrc-Class-Path: ./ substance-6.1.jar trident.jar
Class-Path: .
Rsrc-Main-Class: think.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Well, this is awkward. It turns out that, although my main method was public static, the containing class was not public. Everything is now fixed :)
JarRsrcLoader is included in the Jar when you use Export > Runnable JAR file. It deals with nested jar files in the jar.
JarRsrcLoader is trying to find your main method of think.Main but can't find a method called main which is public static.

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

Why won't my program find the main class correctly?

I'm making a program in java which is just a basic J based gui program. when i compile it in eclipse using the play button, it runs perfectly and there are no bugs or errors to be seen.
But when i export it and run it, it just returns an error saying the main class cannot be found, there shouldn't be a problem because i have the launch configurations set up correctly and for some reason this only happens when im programming in java 1.7, when i do it in 1.6 it compiles and runs perfectly. even when i export it.
How I export
I just run it by opening to jar file
Inside my MANIFEST.MF file:
Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: net.undeadminecraft.wgh.main.Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
The error message i get
My packages (if it helps)
Run your JAR on the console (java -jar yourjar.jar) and post the full stack trace. Also post output of java -version and javac -J-version.
My guess is that your default Java is Java6, and if you build with Java7, you are using the new Java7 class file format, or your main class contains a reference to a class not present in Java6 (in a place where verification cannot be deferred).
EDIT: Use Process Explorer or another task manager that shows exe paths and that can find process for a window, and show which javaw.exe exactly shows you that error message. Then try the java.exe in same directory to reproduce the error message. Or uninstall all your JRE (JDK may stay) but one, so it is clear which one is running.
I would suggest to set your main class as the "Main-Class" in the MANIFEST.MF:
Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Main-Class: net.undeadminecraft.wgh.main.Main
Could solve your problem, as the main class you had in there is an internal class by Eclipse.
Your main method signature is not correct. Please verify that it's like below :
public static void main(String args[]){
}
You may be missing something. Otherwise please give us your method declaration.

Categories