Can't find or load main class, in a .jar file - java

Been looking for around 2 hours for a solution to my problem, tried countless solutions and still the problem remains unsolved.
I have a game project with few packages; geometry, levels, decoration, and game. Inside the game package resides a gameRun.java
package game;
public class gameRun {
/**
* Program entry point.
* #param args the arguments for the levels
*/
public static void main(String args[]) {
... some code
}
}
and I have the following manifest file :
Main-Class: game.gameRun
Class-Path: someExternalGraphicTool.jar
//newline here
I compile my whole game using the following command : (the bin folder already exists).
javac -d bin src/geometry/*.java src/decoration/*.java src/levels/*.java src/game/*.java
I then turn my compiled classes into an executable .jar file using the command :
jar cvfm t.jar manifest.mf bin/geometry/*.class bin/decoration/*.class bin/levels/*.class bin/game/*.class
I then try to run my t.jar using :
java -jar t.jar
//or
java -cp .:t.jar game.gameRun
which both produce the error, "Error: Could not find or load main class game.gameRun"
However, when I unzip the jar file, inside the bin/game directory I can see a gameRun.class file and inside the META-INF\MANIFEST.MF file I can still see that the Main-Class is set to game.gameRun.
Note that I am working on a remote linux server, on a command line, and the jar file doesn't work even if I download it to my windows 10 machine.
What did I do wrong during this process ? Thanks for any help.

Your classes are in a wrong structure inside a jar file, because of the bin folder. My suggestion: pack it all into a jar starting in a bin folder.
Now when you extract your jar you will see 2 folders: META-INF and bin.
If you make the jar from bin folder you will see: META-INF and game, and it will work.
It doesn't work for you simply, because it can't find the main class, since it is inside the bin/game/YourClass.class, not in game/YourClass.class.
...\bin> jar cvfm t.jar manifest.mf geometry/*.class decoration/*.class levels/*.class game/*.class
And then just:
...\bin> java -jar t.jar

Related

Can't figure out how to run my jar file "could not find or load main class"

I'm trying to create a jar file and run it using java -cp main.jar com.test.Foo.Main but I keep getting:
Error: Could not find or load main class com.test.Foo.Main
Caused by: java.lang.ClassNotFoundException: com.test.Foo.Main
This is my file structure. So I'm thinking the line in my Main.java should be package com.test.Foo correct?
I'm compiling my Main.java with javac Main.java which outputs a Main.class file. Afterward, I create a jar file using jar cfm main.jar META-INF/MANIFEST.MF Main.class and finally while I'm in the same directory as the jar file <root>/src/com/test/Foo/ I run java -cp main.jar com.test.Foo.Main and that's when I run into the above error. Any idea how I can run this file like this (and yes I need it to run with this command specifically)?
Main.java
package com.test.Foo;
public class Main {
public static void main (String args[]) {
System.out.println("I am com.test.Foo.Main");
}
}
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.test.Foo.Main
I tried using some of the options given in this popular SO question and nothing helped.
The picture you're showing in your question is your project structure not your jar structure.
When you create a jar file, the structure for that jar file might be
different with your source code folder structure.
Every IDE (such as eclipse, netbeans, IntelliJ) has a mechanism for creating JAR files. In your case when you open the created jar file (using zip apps like winrar) you should see something like this :
com
|
test
|
Foo
|
Main
META-INF
|
MANIFEST.MF
This should be the ordering of your files and folders, otherwise Java can not find your main class from MANIFEST.MF
Now to solve this problem:
Open your jar file using a zip application like winrar
check the folder structure residing inside your jar file as I draw
Fix it right away within the winrar or try to correct your project structure to produce the structure I mentioned.
The class is called com.test.Foo.Main you need to specify the full name in the command:
java -cp main.jar com.test.Foo.Main
or you can use the simpler
java -jar main.jar
Check your META-INF/MANIFEST.MF for the attribute of Manifest-Version: 1.0
This attribute must be there.
Edit:
You need to move to the source root src/ and issue below command to create a valid jar.
javac com/test/Foo/*.java
and, create the jar using,
jar cmf com/test/Foo/MANIFEST.MF main.jar com/test/Foo/*.class
The thing is, package structure should match with the folder structure apparently.

Command line java error: Could not find or load main class

I'm trying to run a project from the command line (Ubuntu 14.04). The main class is called Demo, and I have a Demo.java, a Demo.class, and a Demo$1.class all in the same directory (I know it would be better to seperate them).
I wrote my own Manifest file, MANIFEST.MF, which just looks like this:
Main-Class:Demo
I made sure to end it with a newline.
Next, I want to create my .jar file. I did so with this command:
jar -cfm example.jar MANIFEST.MF *.class
Then, I try to run my project like so:
java -Djava.library.path=/path/to/dependencies -jar example.jar
I seem to get the following error no matter what I try:
Error: Could not find or load main class Demo
I've actually never compiled/run a Java project from the command line before, it's possible I'm making a stupid mistake and just can't figure it out. Any help is appreciated!
EDIT: Here are the contents of example.jar, according to vim:
" zip.vim version v27
" Browsing zipfile /home/ellen/bendersexample2/src/bendersexample/example.jar
" Select a file with cursor and press ENTER
META-INF/
META-INF/MANIFEST.MF
AnnotatedBenders.class
Demo$1.class
Demo.class
Demo$ModelType.class
ManualBenders$1.class
ManualBenders$BendersCallback.class
ManualBenders.class
Model.class
Problem.class
Solution.class
Solution$Verbosity.class
StandardModel.class
Here are the contents of the META-INF/MANIFEST.MF which is in the jar:
Manifest-Version: 1.0
Created-By: 1.8.0_161 (Oracle Corporation)
Main-Class: Demo
UPDATE: Here are the interesting parts of Demo.java:
package bendersexample;
public final class Demo {
/* Some functions */
public static void main(final String[] args) {
/* Some code */
}
}
I changed my MANIFEST.MF to the following:
Main-Class:bendersexample.Demo
And re-generated the example.jar file. I still get the following:
Error: Could not find or load main class bendersexample.Demo
Could there be an issue with how I generate my class files?
To generate the class files initially, I did the following:
javac -classpath .:/opt/ibm/ILOG/CPLEX_Studio_Community128/cplex/lib/cplex.jar *.java
Please let me know what else I should try! Thank you
The problem was just that had the Manifest in the /bendersexample folder and the was creating the .jar in this folder as well! I just needed to move that stuff into the parent directory and everything worked fine!
The final Manifest file used bendersexample.Demo as the Main-Class, and the jar was created and run from /bendersexample 's parent directory.
If anyone runs into this problem I would recommend taking a look at your project's structure before trying anything else, because this turned out to be a very easy fix!

Export project as jar file using manifest [duplicate]

This question already has an answer here:
Exporting executable/runnable jar in eclipse
(1 answer)
Closed 7 years ago.
I just wrote Java console project , which i shoud pack as jar file. I googled it alot and found different solutions like (Eclipse) Project -> New Configuration -> e.t.c Then export as jar file using speciefied config. That's nice, but it's not what i need. After making repeatedly - it won't work. I tried to do same as mentioned here: click
Second Solution: create jar using manifest. Well it's much better, because i can specifiy entry point of main.class. But it's won't work due to can;t find out where's annother packages.
Upd: Launcher.class looks like this
package backpack.dao;
public class Launcher{
public static void main(String[] args){
Backpack.start();
}
}
My project structure:
src/backpack/dao/Item.java
/Backpack.java
/Chromo.java
src/backpack/main/launcher/Launcher.java
The Question is: What should i write in manifest instead of this:
Main-Class src/backpack/main/launcher/Launcher to make executable jar
successfully?
P.S Launcher class uses instances from Backpack.java
Please don't downgrade. I'm rookie
Thanks
What is the package of the Launcher.java? Have you included package name to the classname in manifest.txt?
Remember, when Launcher.java is in package "backpack.main.launcher" your classname in manifest.txt will be:
Main-Class src/backpack.main.launcher.Launcher
instead of:
Main-Class src/backpack/main/launcher/Launcher
Also, remember about this:
The text file must end with a new line or carriage return.
To check what exactly is wrong with your jar file run it in the command line using:
java -jar MyJar.jar
and paste here output with error message.
For more help you can check this example from Oracle: link
Edit:
I recreated your project structure and I have found the solution (at least I think I have :) )
Compile your code and go to the parent directory with compiled code (with *.class files, this should be "out" directory or something like this - depending from your IDE). For example: when your project is in some/path/src/backpack/main/launcher go to src directory.
Run command:
jar cvfe MyJar.jar backpack.main.launcher.Launcher -C . .
(Yes, two dots at the end)
This should create jar with name MyJar.jar and manifest file (-e flag) with main class backpack.main.launcher.Launcher. -C flag include all *.class files recursively from directory . (current one), so both from dao and launcher package.
Test jar: java -jar MyJar.jar

file not found exception when trying to create jar file

I have the following Java file(apples.java):
public class apples
{
public static void main(String[] args)
{
System.out.println("Apples here.");
}
}
saved in the source folder of the MyProject directory.
I compile apples.java and save the apples.class file into the classes folder of the MyProject directory.
I then create manifest.txt with the following content:
Main-Class: apples
I then navigate into the MyProject/classes directory via cmd prompt(Windows XP) and type the following command:
jar -cvmf manifest.txt app1.jar apples.class
I get the following message in the command prompt:
java.io.FileNotFoundException: manifest.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
What is wrong and how do I fix it?
Put the .jar argument first
jar -cvmf app1.jar manifest.txt apples.class
Make sure both "apples.class" and "manifest.jar" are in the current directory.
I would also encourage you to:
1) Use packages (instead of the default package)
2) Capitalize your class names ("Apples.java" instead of "apples")
Here's a nice, short tutorial that might help:
http://www.mkyong.com/java/how-to-add-your-manifest-into-a-jar-file/
It is a common mistake people make. While creating the manifest.txt, make sure that you don't name it "manifest.txt", after you've already selected the file to be a txt. That makes it "manifest.txt.txt". Hope it helps.

CLASSPATH 101... (on Windows)

I'm new to working with Java from the command line and I don't get it. I read previous CLASSPATH questions but still didn't get my problem to work.
I have the following class in C:\Temp\a\b\c
package a.b.c;
public class Hello
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
The package name is intentional.
I compiled it fine and I put the Hello.class file inside C:\Temp\a\target
Now in the command line I go to C:\Temp\ and execute the following:
java -cp .\a\target a.b.c.Hello
It complains that it cannot find the class a.b.c.Hello
Thanks in advance!!
and I put the Hello.class file inside C:\Temp\a\target
This is wrong. It should be placed in the same folder as the .java file. The source code itself is declared to be in the package a.b.c; so, the .class file should really be kept in \a\b\c folder.
Then, to execute it just do:
C:\Temp>java -cp . a.b.c.Hello
Avoid "putting" the classfiles anywhere. The following should work:
javac -d c:\temp c:\temp\a\b\c\Hello.java
# creates Hello.class in c:\temp\a\b\c
java -cp c:\temp a.b.c.Hello
To expand on BalusC's point: the classpath defines a "root". When java is looking for your classes, it will start at each root (or jar) in your class path and drill down through the directories to match the package strucutre. You still need to have you class in a directory structure that matches its package name. In your case, to execute
java -cp .\a\target a.b.c.Hello
you would move the file to
.\a\target\a\b\c\Hello.class
Years ago, I too found this baffling.
Java will try to search for a directory structure a\b\c from starting in target and as you notice, it wont work.
Move the whole directory into target and you'll be fine, it should look like:
C:\Temp\a\target\a\b\c\Hello.class
You may compile it with the -d option which tall the compiler where to put the class file.
Many project structures are like this.
C:\whatever\projectname\src
C:\whatever\projectname\classes
C:\whatever\projectname\bin
C:\whatever\projectname\lib
C:\whatever\projectname\doc
That way you can always step on your project directory and type:
javac -d classes src\*.java
Which will compile all the sources in the src directory and will place them in the classes directory.
Then execute your program:
java -cp classes a.b.c.Hello
You may optionally place required jars in lib
This works pretty fine for small programs ( < 10 src files and 2 - 3 jar libraries ) If it grows beyond that, you could probably use an IDE or ant
The good thing about following this project structure is that some IDES ( as IntellJ idea ) just pick them very easily when you create a new project. You select "Create project from existing sources" and then you can continue from there.
I like compiling and editing at the command line a lot!!

Categories

Resources