71

I'm trying to load a jar using

@echo off
java -jar Test.jar
pause

With the manifest of

Manifest-Version: 1.0
Main-Class: classes.TestClass

In the Jar directory, I can clearly see a classes\TestClass file when I extract it.

Edit: classes.TestClass does have a public static void main(String[] args).

Package Deceleration in classes.TestClass is package classes;

But I still keep getting the error message

Could not find or load main class classes.TestClass

I've been through everything I've been able to find with this problem, and none of it seems to help.

I've tried editing the classpath, redoing the manifest, installing the new JRE.

What else should I be doing?

jww
  • 97,681
  • 90
  • 411
  • 885
Austin
  • 4,801
  • 6
  • 34
  • 54

22 Answers22

40

I got it working like this:

TestClass.Java

package classes;

public class TestClass {

    public static void main(String[] args) {
        System.out.println("Test");
    }

}

Use javac on the command line to produce TestClass.class. Put TestClass.class in a folder classes/.

MANIFEST.MF

Manifest-Version: 1.0
Main-Class: classes.TestClass

Then run

jar cfm test.jar MANIFEST.MF classes/

Then run it as

java -jar test.jar
BeRecursive
  • 6,286
  • 1
  • 24
  • 41
  • 7
    I'd like to remark a point implicit in the answer: it's important to "cd" to the path which would have been used as the classpath was TestClass.class ran without a jar file. For instance, "cd /MyEclipseWorkspace/MyProject/bin" If, instead, jar uses the absolute path of the class files it may fail with "could not find or load main class" error. For instance, this is wrong: jar cfm test.jar manifest.txt /absolutepath/classes/TestClass.class – Corral Nov 24 '15 at 11:41
  • 1
    @Corral's comment should not be ignored. I was pulling my hair out trying to solve this problem until I read it, and now my jar runs perfectly. – Kyle G. Aug 03 '16 at 19:29
  • 1
    `>%JAVAHOME%\bin\jar cfM MANIFEST.MF test.jar classes/` results in `test.jar : no such file or directory` and `MANIFEST.JS` is now broken (its binary file now). – Peter Zhukov Oct 23 '17 at 11:07
31

This error comes even if you miss "-" by mistake before the word jar

Wrong command java jar test.jar

Correct command java -jar test.jar

mGeek
  • 786
  • 8
  • 11
21

java -cp "full-path-of-your-jar" Main

to run any other class having "public static void main" in some package,

java -cp "full-path-of-your-jar" package1.package2.packages-hierarchy.ClassHavingMain

pb2q
  • 58,613
  • 19
  • 146
  • 147
Kalidas Y
  • 221
  • 2
  • 2
12

1.Create a text file calles Manifest.txt and provide the value as

Main-Class: classes.TestClass

2.Create the jar as

jar cfm test.jar Manifest.txt classes/*.class

3.Run the jar as

java -jar test.jar

Arham
  • 2,072
  • 2
  • 18
  • 30
  • Could you tell how's your java path and classpath set. Just one thing you can try before replying. Try running the same TestClass via command line instead of Eclipse. – Arham Oct 23 '12 at 12:58
  • I can run the class just fine using command line. Along with other java commands like javac. – Austin Oct 23 '12 at 12:59
11

I know this is an old question, but I had this problem recently and none of the answers helped me. However, Corral's comment on Ryan Atkinson's answer did tip me off to the problem.

I had all my compiled class files in target/classes, which are not packages in my case. I was trying to package it with jar cvfe App.jar target/classes App, from the root directory of my project, as my App class was in the default unnamed package.

This doesn't work, as the newly created App.jar will have the class App.class in the directory target/classes. If you try to run this jar with java -jar App.jar, it will complain that it cannot find the App class. This is because the packages inside App.jar don't match the actual packages in the project.

This could be solved by creating the jar directly from the target/classes directory, using jar cvfe App.jar . App. This is rather cumbersome in my opinion.

The simple solution is to list the folders you want to add with the -C option instead of using the default way of listing folders. So, in my case, the correct command is java cvfe App.jar App -C target/classes .. This will directly add all files in the target/classes directory to the root of App.jar, thus solving the problem.

Log2
  • 142
  • 2
  • 5
  • 4
    This os the only answer that helped me! THANK YOU. Is there an other way than going (cd) into the folder with the packages/classes? – noctua May 01 '19 at 10:13
  • I've gone through dozens of different threads and docs on this topic. Everything was useless except your answer. Thank you. – Jason Kim May 10 '20 at 05:14
  • @noctua You should look int `-C` option. `jar cfmv Jarfilename.jar manifest.txt ./classes/PackageName -C classes .` This will help you avoid `cd` into the classes directory. On `-C` option, https://docs.oracle.com/javase/tutorial/deployment/jar/view.html – Jason Kim May 10 '20 at 05:53
  • @JasonKim that is literally the last thing I talk about in my answer! – Log2 Jul 24 '20 at 16:13
  • 1
    @Log2 I know, but look at what noctua is asking in the comment. He may not have read the last thing in your answer. And he asked the question that would be answered by what you pointed out and I repeated. I was trying to to be helpful for noctua. I also thanked you in the comment as well because your answer was the only one that was useful. – Jason Kim Jul 24 '20 at 18:21
  • 1
    @JasonKim sorry, I didn't realize you're just repeating it for him. I didn't mean to call you out or anything like that. – Log2 Sep 14 '20 at 10:50
  • @Log2 No problem – Jason Kim Sep 14 '20 at 19:19
8

I had the same problem due to copying and pasting code from a Microsoft Word Document. Seems there was a slightly different type of dash - character used, when I replaced the longer dash character with the correct - the program executed properly

Max Carroll
  • 4,441
  • 2
  • 31
  • 31
7

A possible reason for this error message:

Could not find or load main class

is when the main class extends (or implements) another class (or interface), which is not on the classpath.

Stéphane Millien
  • 3,238
  • 22
  • 36
Gabor Umann
  • 101
  • 1
  • 1
4

This is very difficult to debug without complete information.

The two most likely-looking things at this point are that either the file in the jar is not stored in a directory WITHIN THE JAR, or that it is not the correct file.

You need to be storing TestClass.class - some people new at this store the source file, TestClass.java.

And you need to create the jar file so that TestClass.class appears with a path of classes. Make sure it is not "/classes". Use zip to look at the file and make sure it has a path of "classes".

arcy
  • 12,845
  • 12
  • 58
  • 103
3

I had a similar problem which I could solve by granting execute-privilege for all parent folders in which the jar-file is located (on a linux system).

Example:

/folder1/folder2/folder3/executable.jar

all 3 folders (folder1, folder2 and folder3) as well as the executable.jar need execute-privilege for the current user, otherwise the error "Could not find or load main class ..." is returned.

Alex
  • 947
  • 9
  • 13
3

I had a weird issue when an incorrect entry in MANIFEST.MF was causing loading failure. This was when I was trying to launch a very simply scala program:

Incorrect:

Main-Class: jarek.ResourceCache
Class-Path: D:/lang/scala/lib/scala-library.jar

Correct:

Main-Class: jarek.ResourceCache
Class-Path: file:///D:/lang/scala/lib/scala-library.jar

With an incorrect version, I was getting a cryptic message, the same the OP did. Probably it should say something like malformed url exception while parsing manifest file.

Using an absolute path in the manifest file is what IntelliJ uses to provide a long classpath for a program.

Jarekczek
  • 7,456
  • 3
  • 46
  • 66
2

At least the way I've done this is as follows:

If you have a nested src tree (say com.test.myclass.MyClass) and you are compiling from a root directory you need to do the following:

1) when you create the jar (usually put this in a script): jar -cvfm my.jar com/test/myclass/manifest.txt com/test/myclass/MyClass.class

2) The manifest should look like:

Mainfest-version: 1.0 Main-Class: com.test.myclass.MyClass Class-Path: . my.jar

3) Now you can run the jar from anywhere like this:

java -jar my.jar

Hope this helps someone

Ramjet
  • 43
  • 1
  • 7
2

I follow the following instruction to create a executable .jar in Eclipse. Then Run command "java -jar .jar " to launch the program.

It takes care of creating mainfest and includeing main class and library files parts for you.

http://java67.blogspot.com/2014/04/how-to-make-executable-jar-file-in-Java-Eclipse.html

user1457659
  • 4,514
  • 1
  • 15
  • 5
2

If you use package names, it won't work with folders containing dots (Error: Could not find or load main class). Even though it compiles and creates the jar file successfully.

Instead it requires a complete folder hierarchy.

Fails:

com.example.mypackage/
    Main.java

Works:

com/
    example/
        mypackage/
            Main.java

To compile in Linux:

javac `find com/ -name '*.java'` \ && jar cfe app.jar com.example.mypackage.Main `find com/ -name '*.class'`

gregn3
  • 1,728
  • 2
  • 19
  • 27
2

in IntelliJ, I get this error when trying to add the lib folder from the main project folder to an artifact.

Placing and using the lib folder in the src folder works.

Darcia14
  • 214
  • 1
  • 13
1

I had this error because I wrote a wrong Class-Path in my MANIFEST.MF

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
bb1950328
  • 1,403
  • 11
  • 18
1

You need to use -jar argument to set the path to your jar:

java -jar your_jar.jar

You don't need to set main class manualy if your manifest is correct.

Acuna
  • 1,741
  • 17
  • 20
0

I had this error because I extracted JAR files from libraries to the target JAR by IntellijIDEA. When I choose another option "copy to the output directory...", it solved the problem. Hope this helps you

DEN
  • 11
0

I was getting this error because my main class is in test package, and I am creating artifact using IntelliJ. After I checked the box Include Tests when creating artifact, it got resolved.

Screenshot from IntelliJ showing Include Tests checkbox

Venkat
  • 1
  • 1
0

Sometimes could missing the below line under <build> tag in pom.xml when packaging through maven. since src folder contains your java files

<sourceDirectory>src</sourceDirectory>
Mohemmed Niyaz
  • 214
  • 2
  • 8
0

If you have your setup with Maven project -- make sure that the following match your JDK in the pom.xml file.

<maven.compiler.source>17</maven.compiler.source>

<maven.compiler.target>17</maven.compiler.target>

in my case I had it set to 14 whereas my application was set to 14 -- and the conflict was causing my jar file to give the class not found error.

MWiesner
  • 8,868
  • 11
  • 36
  • 70
0

For IntelliJ users - this can also happen when class that cannot be found was placed in a directory marked as Tests (indicated by green color on directory icon).

To solve this:

a. move the class to sources directory (prefered)

or

b. make sure "include tests" checkbox is selected when creating an artifact

nuclear_party
  • 91
  • 1
  • 5
0

I faced the same issue while using artifact in IntelliJ. After using copy all dependency jar, it works for me.