Java: Could not find the main class. Program will exit - java

Works on my PC. Doesn't work on the 2 others I tried: they get the entitled error.
Manifest.txt:
Manifest-Version: 1.0
Class-Path: .
Main-Class: core.Initiator
^ extra line at the end.
Hierarchy:
Project.jar
core
Initiator
...
images
...
META-INF
MANIFEST.MF
CreateJar.bat:
cd bin
jar cvfm ..\Project.jar ..\Manifest.txt *
pause
Run.bat:
#echo off
start javaw -jar Project.jar

From the comments in the actual question. This could be because of the relative paths that you are using.
Also, does your jar file contain the images or is it just lying somewhere in the workspace? This would again suggest that the relative paths may be wrong.

The computers with the error needed to update their JDK.

Related

Executable jar not running? Windows 10

I am having various .jar files in my system.
I have the JDK and JRE installed.
Most of jar files run on double clicking, but there are 2 - 3 jar files which do nothing on clicking. Help me.
By the way I am using windows 10 64 bit
Problem:
I could not start JAR files just by clicking on them in Windows 10.
I was messing around with the "control panel" ... forget it.
What I've found out:
Start a command line (cmd) as an Administrator
Check your file type association:
ftype jarfile
assoc .jar
I had to change my java path to a different one
ftype jarfile=C:\myjavapath\javaw.exe -jar "%1" %*
Which basically means, that if someone starts a jar file, the command would be:
C:\myjavapath\javaw.exe -jar "example.jar" parameter1 parameter2
For me, I also had to change my .lnk files to only contain the name of the jar file, not the whole command. Type in the whole path of the jar file in the "target" field in the properties of the link file (.lnk).
You can debug it in the Command Prompt.
Open start, type in CMD.exe, hit enter
Then, type in
java -jar "path\to\file.jar" without the quotes
or
java "path\to\file.jar"
You should be able to see an output log of what is happening that is making the jar file not execute properly
Your Manifest file should contain a line:
Main-Class: classname
See "Setting an Application's Entry Point".

When executing JAR, I get Java program completed with exit code 1

I have a java classfile which has 'Hello World'.
I have created JAR file and Manifest file.
When I do a RUNJVA command with Classfile it works fine.
But when I try to use JAR file in RUNJVA, I don't see any output after the call.
Classpath is set properly.
I Just get 'Java program completed with exit code 1'
Thanks in Advance.
Make sure that you have a file META-INF/MANIFEST.MF in your jar file that contains at least something like Main-Class: full.qualified.name.of.MyClass and start it with java -cp {my-classpath-here} -jar myjarfile.jar

Java running external jar without IDE?

I've written a java program that reads an excel file with jxl.jar. It's currently working, but I have to use cmd to run the program. Double clicking the jar file does not appear to be working. These are the commands I use to compile and run the code:
javac -classpath C:/workspace/jxl.jar:. main.java GUi.java
jar cvfm run.jar manifest.txt Main.class GUI.class GUI$1.class GUI$2.class GUI$3.class Main$1MyCustomTableCellRenderer.class Main$1YourTableCellRenderer.class Main$MyCustomTableCellRenderer.class
java -cp run.jar Main
I'm not really sure why it's any different from double clicking it. I've compiled the jxl file into the run.jar file so I don't understand why it doesn't work?
An Example directly from Java Tutorial on Adding Classes to the JAR File's Classpath as suggested by #MadProgrammer in his comment.
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
jar cfm MyJar.jar Manifest.txt MyPackage/*.class
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)
The classes in MyUtils.jar are now loaded into the class path when you run MyJar.jar.
Read more...
Try this in command prompt:
Run the below command if you bundled the jar with all libraries
java -jar MyJar.jar
If you need to add jars at the time of execution please add all the jars at command line, as follows;
java -cp /path/jxl.jar;myJar.jar Class-Name-Main

.jar error - could not find or load main class

I tried to put HelloWorld in a .jar file and running it, but it doesn't work. I created the java file and typed in the program, and then wrote in cmd:
javac HelloWorld.java
java HelloWorld
and it worked. Then I entered
echo Main-Class: HelloWorld >manifest.txt
jar cvfm HelloWorld.jar manifest.txt HelloWorld.class
and got the output
added manifest
adding: HelloWorld.class(in = 426) (out= 288)(deflated 32%)
I then entered
java -jar HelloWorld.jar
HelloWorld.jar
and the first line worked, while the second line gave me an error:
Error: Could not find or load main class path\HelloWorld.jar
which is the same output I got (in a rapidly closing window) when I tried to open it with the java.exe file in 64 bit jre7\bin, jdk1.7.0_51\bin, jdk1.7.0_51\jre\bin, as well as 32 bit jre7\bin. I've uninstalled and reinstalled both my jre and jdk and recreated my .class and .jar files, but the problem persists. I'm on win8.
Edit: I tried to do as aetheria suggested, but no luck. I put HelloWorld.java in path\com\stackoverflow\user\blrp, compiled it, and it worked by entering
java com.stackoverflow.user.blrp.HelloWorld
in path. I then created the manifest and jar by:
(echo Manifest-Version: 1.0
echo Class-Path: .
echo Main-Class: com.stackoverflow.user.blrp.HelloWorld) >manifest.txt
jar cvfm HelloWorld.jar manifest.txt com\stackoverflow\user\blrp\HelloWorld.class
and got the output
added manifest
adding: com/stackoverflow/user/blrp/HelloWorld.class(in = 454) (out= 312)(deflat
ed 31%)
but still, java -jar HelloWorld.jar worked and HelloWorld.jar didn't (same error). I also tried not doing the package thing, just the Class-Path in manifest, same result.
(Also, I solved the problem prior to asking the question by use of a .bat file, but it'd still be sweet to get that jar working.)
Thanks jbaliuka for the suggestion. I opened the registry editor (by typing regedit in cmd) and going to HKEY_CLASSES_ROOT > jarfile > shell > open > command, then opening (Default) and changing the value from
"C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
to
"C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*
(I just removed the w in javaw.exe.) After that you have to right click a jar -> open with -> choose default program -> navigate to your java folder and open \jre7\bin\java.exe (or any other java.exe file in you java folder). If it doesn't work, try switching to javaw.exe, open a jar file with it, then switch back.
I don't know anything about editing the registry except that it's dangerous, so you might wanna back it up before doing this (in the top bar, File>Export).
I found this question when I was looking for the answer to the above question. But in my case the issue was the use of an 'en dash' rather than a 'dash'. Check which dash you are using, it might be the wrong one. I hope this answer speeds up someone else's search, a comment like this could have saved me a bit of time.
You can always run this:
java -cp HelloWorld.jar HelloWorld
-cp HelloWorld.jar adds the jar to the classpath, then HelloWorld runs the class you wrote.
To create a runnable jar with a main class with no package, add Class-Path: . to the manifest:
Manifest-Version: 1.0
Class-Path: .
Main-Class: HelloWorld
I would advise using a package to give your class its own namespace. E.g.
package com.stackoverflow.user.blrp;
public class HelloWorld {
...
}
Had this problem couldn't find the answer so i went looking on other threads, I found that i was making my app with 1.8 but for some reason my jre was out dated even though i remember updating it. I downloaded the lastes jre 8 and the jar file runs perfectly. Hope this helps.

Running jar from different directory cannot find required dependency

I'm trying to run a jar ec/mobat/MOBAT.jar which depends on some jars located in ec/mobat/lib/. It works if I do:
ec/mobat/$ java -jar MOBAT.jar
However I want to be able to run the jar from another directory
ec/$ java -jar mobat/MOBAT.jar
But I get an exception
java.lang.NoClassDefFoundError: ibis/io/Serializable
...
I tried to pass the required jars in the classpath
ec/$ CLASSPATH=... java -jar mobat/MOBAT.jar
ec/$ java -jar -cp ... mobat/MOBAT.jar
but I get exactly the same exception. Any fix?
Update: MANIFEST.INF contains the following:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: Selmar Kagiso Smit
Main-Class: mobat.Launcher
Implementation-Version: 1.3.4
The classpath has to contain every jar you're depending on.
java -classpath b.jar;c.jar -jar a.jar //does not work see below
The ";" is system dependent for windows ":" for unix.
The jar switch is used to select the jar file whose main class is executed (Main-Class: mobat.Launcher in the manifest file). The command line:
java -classpath b.jar;c.jar;a.jar mobat.Launcher
Would produce the same result.
Alternatively classpath definitions can be added to the Manifest file. Your manifest file could contain the attribute.
Class-Path: lib/b.jar lib/c.jar
Then
java -jar a.jar
would work.
Edit:
I thought that -jar and -cp could be used together. But the java tools documentation is clear:
-jar
When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
Only the manifest and everything explict (classpath and main class) versions work.
You can not use -cp and -jar together
java -cp myjar.jar;lib/*;. mypackage.MyClass
shall work on windows and
java -cp myjar.jar:lib/*:. mypackage.MyClass
shall work on Unix

Categories

Resources