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
Related
I'm using Windows 10. Currently trying to figure out how to work with java files from the console. Bumped into a problem - I have 3 .java files and .\bin directory where all my .class files are stored. I want to create a .jar file, put all .class files in it and make .\bin\Test1.class file an entry point for this archive
So I tried this command that I found on Oracle docs
jar cfe app.jar bin/Test1 bin/*.class
It worked and I got everything I need in this archive but when I try to run the application by
java -jar app.jar
the error appears and tells that It could not find the main class
Error text
So I tried using different delimiters in command and manifest file, added Class-Path: attribute in manifest (however not sure what exactly should be in it), tried running from administrator. Nothing works
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!
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 ?
I am running Windows 7 64-bit.
I would like to make a .bat file that will run my eclipse project's main.class using (preferably) only that .bat file. The project is still undergoing construction so exporting a runnable jar every few minutes isn't an acceptable solution (nor is installing eclipse on all machines). Furthermore I don't really want to install any more plug-ins, I know there is a way to do this with a .bat but haven't been able to figure it out. Here is what the project root folder looks like:
C:/.../ShootEmUp <-inside there we have:
bin/
lib/
natives/
lots of .jar files
res/
src/
ShootEm/
OtherPackages/
ShootEmMain.java
.classpath
.project
RunMe.bat
I have tried more combinations inside the .bat file than I care to admit. Right now RunMe.bat looks like:
#ECHO OFF
javac src.ShootEm.ShootEmMain.java
java -cp lib/*.jar;. src/ShootEm/ShootEmMain;
PAUSE
This currently produces a javac: file not found: src.ShootEm.ShootEmMain.java error
I have also tried putting the .bat inside the folder ShootEmUp/src/ShootEm/ (next to the class with public static void main [ShootEmMain.java]) and editing the RunMe.bat code to:
#ECHO OFF
javac ShootEmMain.java
java -cp ../../lib/*.jar;. ShootEmMain;
PAUSE
This seemed to work a little better as the error I would get then was:
ShootEmMain.java:5: error: package ... does not exist. (the a whole bunch more for all the other jars in the lib folder)
It's useful to note I've already added C:\Program Files\Java\jdk1.7.0_25\bin; to the front of the value of the Path environment variable.
I have also tried classpath instead of cp and a whole crazy list of syntax combinations for each (once I was well past frustrated) to no avail.
Also I realize my .bat example doesn't set the natives as I'm not really sure where that fits into this mess. Thank you in advance!
You should look at using a tool like ant or maven to build your application. This will streamline producing your artifact.
If it's unreasonable to export a working jar across your environment, I boggle at how you could imagine it's more manageable to export your source code around your environment.
I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.