Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Cannot find class in .class or .jar file

  1. #1
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Cannot find class in .class or .jar file

    Dear community,

    One of the frustrating problems I find with programming is when the compiler or runtime environment can’t find stuff when the location to me is blatantly obvious.

    I have followed an introductory course on setting up a Java development environment at Pluralsight, using IntelliJ. At the end was explained in two ways how to create a jar file that is supposed to work stand-alone. The exercise project contains a class called ‘Main’.

    The first method was using the command prompt, but it fails because the runtime environment can’t find or load the class called ‘Main’.
    The second way was using IntelliJ, but it fails for the same reason.

    So I created a new, simpler project, called HelloWorld, with only two files, with also a class called ‘Main’. It fails for the same reason.

    Then I tried some exercise on webeducator (that I can't link to for some reason). It contains only a single file and it actually works, despite the erroneous image in step 4.

    In my HelloWorld project I have two classes :

    package com.example.helloworld;

    public class Main {
     
    public static void main(String[] args) {
        HelloWorldCreator hello = new HelloWorldCreator() ;
     
        hello.giveGreeting() ;
    }
    }

    and

    package com.example.helloworld;
     
    public class HelloWorldCreator {
        String greeting = "Greetings" ;
        String addressee = "World";
     
        public void setGreeting(String greeting) {
            this.greeting = greeting ;
        }
     
        public void setAddressee(String addressee) {
            this.addressee = addressee ;
        }
     
        public void giveGreeting() {
            System.out.printf("%s, %s!", greeting, addressee) ;
        }
    }

    I managed to convert these to .class files using the instruction
    javac Main.java HelloWorldCreator.java
    in the command prompt.

    Issuing the instruction
    java Main
    in the pertinent folder returns
    Error: Could not find or load main class Main
    Caused by: java.lang.NoClassDefFoundError: com/example/helloworld/Main (wrong name: Main)

    How can explain to the JRE where to find that class ?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    The full class name includes the package: com.example.helloworld.Main
    The full name needs to be given to the java command: java com.example.helloworld.Main
    The com folder needs to be in the current directory where the java command is issued so that the java command can find the class file.

    n the pertinent folder
    Your copied contents of the command prompt window does not show what folder the java command was issued in
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    Thanks for your help.

    The folder in which I tried to run the classes is :
    C:\Users\Knarf\IdeaProjects\HelloWorld\src\com\exa mple\helloworld
    That is where they are located.

    Indeed. Moving 3 levels up and instructing
    java com.example.helloworld.Main
    works.

    So the JRE is too stupid to look in the folder where it is being executed.
    However, in order to turn those class files into jar files, the JRE needs to run in the helloworld folder.

    Running the resulting jar file must again be done from src folder.


    I tried the same exercise again, the HelloWorld2 project, with the same code, except for omitting the line
    package com.example.helloworld;
    from each class and not putting the classes in a package.

    That way the JRE can find the Main class (created with IntelliJ) without the extra hassle.

    I make jar files with the commands
    jar -cvf HelloWorldCreator.jar .
    and
    jar -cvf Main.jar .

    These jar files can simply be run from the pertinent folder. No package gymnastics required.

    I am correct in understanding that for such simple programs packaging is merely a nuisance, but that in complicated programs the benefits outway the drawbacks?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    The advantage of using correctly created jar files is that with a properly installed JRE, double-clicking on the jar file will start the java program.
    The jar file can be located anywhere. A Shortcut can be put on the desktop to allow easy access to the program.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    Apparently my JRE is not installed correctly because when I double-click a jar file Windows asks me what I would like to open it with.

    The Pluralsight course also explained how one is supposed to tell the JRE where the main class is within the jar file.

    The location in command prompt is
    C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2\MyPackage>

    So I create a file MANIFEST.MF with
    Main-Class: Mypackage.Main
    (Allegedly one needs to end that line with a new line for it to work.)

    Then I instruct
    jar -cvmf MANIFEST.MF HelloWorldCreator.jar .

    Running HelloWorldCreator.jar that yields
    Error: Could not find or load main class Mypackage.Main
    Caused by: java.lang.ClassNotFoundException: Mypackage.Main

    The content of my jar file is
    META-INF/
    META-INF/MANIFEST.MF
    HelloWorldCreator.class
    MANIFEST.MF
    Main.class

    How can I explain inside the jar file where to find the main class ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    What is the contents of the package statement in the Main.java file?
    The java program is looking for Main in the package Mypackage.

    Note: Java is case sensitive. Mypackage is NOT the same as MyPackage

    Windows ignores case.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    This is the code from the Main class :
    package MyPackage;
     
    public class Main {
     
        public static void main(String[] args) {
            HelloWorldCreator hello = new HelloWorldCreator();
     
            hello.giveGreeting();
        }
    }

    I corrected the typo in the MANIFEST.MF file. It now contains
    Main-Class: MyPackage.Main

    Instructing
    java -jar HelloWorldCreator.jar

    again results in
    Error: Could not find or load main class MyPackage.Main
    Caused by: java.lang.ClassNotFoundException: MyPackage.Main

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    The Main.class file needs to be in the folder: MyPackage inside of the jar file. It looks like neither of the class files inside of the jar file are in the appropriate folder.

    The jar command needs to have the path to the class file like with the java command.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    In Windows Explorer Main.class file is the MyPackage folder.

    “The jar command needs to have the path to the class file like with the java command.”
    What class path with what java command?
    Other explanations I have read and heard failed to mention that. I would also have expected the JDK to be wise enough to put things in the right folder.

    So now I instruct
    jar -cvmf MANIFEST.MF C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2\MyPackage\HelloWorldCreator.jar .

    That results in
    Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2\MyPackage\HelloWorldCreator.jar : no such file or directory
    added manifest
    adding: HelloWorldCreator.class(in = 912) (out= 493)(deflated 45%)
    adding: MANIFEST.MF(in = 28) (out= 29)(deflated -3%)
    adding: Main.class(in = 508) (out= 331)(deflated 34%)

    and no jar files was created.

    Then I try
    jar -cvmf MANIFEST.MF HelloWorldCreator.jar C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2\MyPackage

    That results in
    C:\Users\Knarf\Documents\School\Informatica\Softwa re : no such file or directory
    Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2\MyPackage : no such file or directory
    added manifest

    Then I try
    jar -cvmf MANIFEST.MF MyPackage.HelloWorldCreator.jar

    That creates MyPackage.HelloWorldCreator.jar, which fails to run because JRE can’t find the class MyPackage.HelloWorldCreator.jar.

    Do you know a website where is explained how to do this stuff ?

    Reading https://docs.oracle.com/javase/tutor...ar/appman.html I also tried
    jar -cvmf MANIFEST.MF Main.jar MyPackage/*.class

    but that results in
    MyPackage\*.class : no such file or directory
    added manifest
    Last edited by Amoranemix; August 28th, 2022 at 04:30 AM. Reason: tried something extra

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    I can not find a simple command on my system that does what you are trying to do. I make batch files for many of my projects to create the jar file.
    Here are some examples:
    @REM Make jar file 
    jar -cmf CopyUniqueViolations.mnf CopyUniqueViolations03_06_2021.jar CopyUniqueViolations*.class  org/*
    MORE
    Here is a more complicated one without using a package:
    Rem           Create the BridgeSummary.jar file:
    @REM
    set JarName=%DEV_HOME%\JavaDevelopment\Misc_Projects\BridgeSummary\BridgeSummary.jar
    %DEV_DRIVE%
    cd %DEV_HOME%\JavaDevelopment\Misc_Projects\BridgeSummary
    jar -cmf BridgeSummary.mnf %JarName% *.class
    @Rem Now get NormsTools stuff
    cd %DEV_HOME%\JavaDevelopment\
    jar -uf %JarName% NormsTools\AppletHelper*.class NormsTools\AppletWrapper*.class 
    Echo ----- Created %JarName% -----
    More

    Here is one with a package:
     
    Rem           Create the CmdHelper.jar file:
    @REM
    set JarName=%DEV_HOME%\JavaDevelopment\Misc_Projects\CmdHelper\CmdHelper.jar
    %DEV_DRIVE%
    cd %DEV_HOME%\JavaDevelopment\
    jar -cmf Misc_Projects\CmdHelper\CmdHelper.mnf %JarName% Misc_Projects\CmdHelper\CmdHelper*.class
    @rem get icons
    cd %DEV_HOME%\JavaDevelopment\Misc_Projects\CmdHelper
    jar -uf %JarName% icons\*.png
    @Rem Now get NormsTools stuff
    cd %DEV_HOME%\JavaDevelopment\
    jar -uf %JarName% NormsTools\EditableList*.class NormsTools\EditFields*.class 
    Echo ----- Created %JarName% -----
    More
    The manifest:
     
    Manifest-Version: 1.0
    Main-Class: Misc_Projects.CmdHelper.CmdHelper

    Basically the manifest file must have the full path to the class file with the main method. The classes must be located in the folder reflecting the full class name including the package.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    I still don’t know how to create a working standalone jar-file from my simple program using the JDK alone. Apparently that is overly complicated if you are not doing that frequently.

    So, I try it with IntelliJ. I try to follow the instructions on http://www.jetbrains.com/help/idea/c...n_packaged_jar and lo and behold, the HelloWorld2.jar created that way actually works.

    The content of the jar is
    META-INF/MANIFEST.MF
    META-INF/
    MyPackage/
    MyPackage/HelloWorldCreator.class
    MyPackage/Main.class

    That is different than what was created by the JDK (see post 5). There is probably a way to convince the JDK to create such jar file, but I don’t know how.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    The posted contents of the commands that you entered did not show what directory they were entered in making it hard to judge.
    The jar command needs to be executed in the folder containing the MyPackage folder. The reference to the class would start with the package name
    jar ... MyPackage/*.class ...
    that would include the package name in the jar file.
    MyPackage/HelloWorldCreator.class
    MyPackage/Main.class
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    I’ll give it one more try.

    I am in
    C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2\MyPackage

    It contains
    29/08/2022 11:09 912 HelloWorldCreator.class
    29/08/2022 11:09 508 Main.class
    29/08/2022 18:12 26 MANIFEST.MF

    I instruct
    jar -cvmf MANIFEST.MF Main.jar MyPackage/Main.class

    Response :
    MyPackage\Main.class : no such file or directory
    added manifest

    No matter what I type, it doesn’t work.

    Edit :

    I also tried copying MANIFEST.MF one level up and instructing
    jar -cvmf MANIFEST.MF HelloWorld.jar MyPackage/*.class

    That creates the jar file, but JRE can’t run it because it can’t find or load the main class jar.
    Last edited by Amoranemix; August 29th, 2022 at 12:06 PM. Reason: added something

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    can’t find or load the main class jar.
    What did you do to get that message?

    Here is one way to get that error message:
    C:\Users\Norm>java jar
    Error: Could not find or load main class jar
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    I have still the commend prompt window open, so I can copy the instruction I gave :
    C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2>java jar HelloWorld.jar

    Response :
    Error: Could not find or load main class jar
    Caused by: java.lang.ClassNotFoundException: jar

    The contents of the HelloWorld2 folder is
    29/08/2022 19:02 <DIR> .
    29/08/2022 19:02 <DIR> ..
    29/08/2022 19:02 1.456 HelloWorld.jar
    29/08/2022 18:12 26 MANIFEST.MF
    29/08/2022 11:09 <DIR> META-INF
    29/08/2022 19:02 <DIR> MyPackage

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    Options passed to the java command need to start with a -
    java -jar TheJarFIlename.jar
    Otherwise the java command thinks it is the name of a class -
    Error: Could not find or load main class jar
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    OK, so I instructed
    C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2>java -jar HelloWorld2.jar

    Response :
    Error: Unable to access jarfile HelloWorld2.jar

    I tried adding MyPackage\ and MyPackage. in front of HelloWorld2.jar, to no avail

    HelloWorld2.jar is in C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\artifacts\Hel loWorld2_jar

    Running it there works, but that is the version produced by IntelliJ

    The version in C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\production\He lloWorld2 doesn’t work :
    no main manifest attribute, in HelloWorld.jar

    That folder contains
    29/08/2022 19:02 <DIR> .
    29/08/2022 19:02 <DIR> ..
    29/08/2022 19:02 1.456 HelloWorld.jar
    29/08/2022 18:12 26 MANIFEST.MF
    29/08/2022 11:09 <DIR> META-INF
    29/08/2022 19:02 <DIR> MyPackage

    HelloWorld.jar contains
    META-INF/
    META-INF/MANIFEST.MF
    MyPackage/HelloWorldCreator.class
    MyPackage/Main.class

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find class in .class or .jar file

    C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2>java -jar HelloWorld2.jar

    Error: Unable to access jarfile HelloWorld2.jar
    When using: java -jar HelloWorld2.jar
    the command needs to be issued in the folder containing the jar file.

    HelloWorld2.jar is in C:\Users\Knarf\Documents\School\Informatica\Softwa re Testing\IdeaProjects\HelloWorld2\out\artifacts\Hel loWorld2_jar
    That is where the java command needs to be issued.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Aug 2022
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find class in .class or .jar file

    OK. Thanks for your help.

Similar Threads

  1. Replies: 4
    Last Post: October 26th, 2019, 08:44 PM
  2. Execution of .jar file: Could not find main class....Error'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 14th, 2012, 02:04 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. Find the important class in ".mse" file using Java program
    By ashwarth21 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 13th, 2012, 07:43 PM

Tags for this Thread