Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Java giving me headaches: "Could not find or load main class" error while it is there

    Hey guys,

    I'm not new to programming, but I am new to Java and I have to tell you it is giving me a headache. I'm using Deitel and Deitel's How to Program Java 10th edition book to teach myself Java after already programmed in C++ and Object Pascal. I've been reading a lot about Java CLASSPATH, classes etc. and it was working before. I've written a program with Netbeans:

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    package newpackage;

    /**
    *
    * @author Danny
    */
    public class Welcome1
    {
    //main method begin execution of Java application
    public static void main(String[] args)
    {
    System.out.println("Welcome to Java Programming!");
    } //end method main
    }//end Class Welcome 1



    As an exercise I've compiled the class using the Javac command in cmd.
    There is now a Welcome1.java, and a Welcome1.class in the same directory. C:\Users\...\NetBeansProjects\Welcome1\src\newpack age
    My CLASSPATH is set at ".;". While I'm in the correct folder in cmd, when I try to run Java Welcome1, it is giving me the error "Could not find or load main class Welcome1.
    Hope somebody can help me out!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    What folder are you in on the command prompt? You should be in the src directory.

    How have you compiled the class? Post the exact text here.

    How are you trying to run the class? Post the exact text here.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Does this help?


    C:\Users\...\Documents\NetBeansProjects\Welcome1\s rc\newpackage>javac Welcome1
    .java

    C:\Users\...\Documents\NetBeansProjects\Welcome1\s rc\newpackage>dir
    Volume in drive C has no label.
    Volume Serial Number is 0A21-950B

    Directory of C:\Users\...\Documents\NetBeansProjects\Welcome1\s rc\newpackage

    31-07-2014 15:13 <DIR> .
    31-07-2014 15:13 <DIR> ..
    31-07-2014 16:28 449 Welcome1.class
    29-07-2014 16:28 475 Welcome1.java
    2 File(s) 924 bytes
    2 Dir(s) 50.075.439.104 bytes free

    C:\Users\...\Documents\NetBeansProjects\Welcome1\s rc\newpackage>java Welcome1
    Error: Could not find or load main class Welcome1

    C:\Users\...\Documents\NetBeansProjects\Welcome1\s rc\newpackage>

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    It does. Your class has a package so the fully qualified name becomes newpackage.Welcome1 (you should use more appropriate names).

    To run your class, you need to ensure two things.

    1) the folder c:\users\bla\Documents\NetbeansProjects\Welcome1\s rc is on the classpath
    2) you run with the fully qualified classname

    ex:

    Java Code:
    java -cp C:\Users\...\Documents\NetBeansProjects\Welcome1\src newpackage.Welcome1
    That will work anywhere, you don't have to be any specific directory. To simplify it, you can just navigate to the 'src' directory and then just do this:

    Java Code:
    java newpackage.Welcome1
    since by default Java will add the current working directory to the classpath.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Thank you for your info! I very much appreciate it. I will test it as soon as I get home.
    I do have 2 more questions if you don't mind.

    1. I did not know about these packages. Nor do I fully understand what it is doing. It was just hinted in a Netbeans intro movie that one needs a package. Can you maybe explain to me how this works?
    2. If CLASSPATH is set to ".;", doesn't that mean that Java will look for the class in the same directory? Ergo, one should not need all of this stuff?

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    I'm not going to spend a good deal of time explaining something which is documented in hundreds of sources on the net and in literally all books on core Java programming.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Think of it this way: if you have a class Foo in package bar, its fully qualified class name if bar.Foo. The system class loader translates packages to directories and the classpath list lists the directories where to look for classes; so, if your class is stored in directory /baz/bar/Foo.class, the classpath should point to /baz where the classloader will find a class bar.Foo.

    kind regards,

    Jos (<--- the good cop ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Quote Originally Posted by JosAH View Post
    Jos (<--- the good cop ;-)
    Well today you are ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Quote Originally Posted by gimbal2 View Post
    Well today you are ;)
    I'm a Samaritan and I should be holy and rewarded with ample supplies of tobacco, espresso coffee and loads of Grolsch beer, that's all I ask; now go, you simple mortal and fetch me those errants.

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Yes Sahib. My feet are like the wind Sahib.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Hey fellow dutchies, I really appreciate you explaining this all to me. Deitel and Deitel have not yet explained to me how Java uses packages, so if it's correct that all books explain this, I guess I will read about it later on. They have told me about CLASSPATHs and that putting .; as standard value would result in Java looking for the class in the current directory. I suppose that is correct?
    P.S. My girlfriends mom has a bar that serves Grolsch. Grolsch rocks ;)

  12. #12
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Ah you are reading a book, very good. But how old is that book you are reading exactly? It's quite old-fashioned information. It works, but since Java 6 it is not necessary.

    You know what a "." is in filesystem terms, right? "the current working directory". And ".." is "one directory up in the hierarchy". So yes, using that as the classpath manually sets it to the current working directory. Although you can drop the ';' as that is used to separate different classpath elements (on Windows).

    Java Code:
    java -cp . bladiebla
    And if you would want to add the current directory AND c:\explosivebob to the classpath, you'd on Windows do:

    Java Code:
    java -cp .;c:\explosivebob bladiebla
    And on Linux/Mac you'd do:

    Java Code:
    java -cp .:/usr/home/explosivebob bladiebla
    because the separator character depends on the operating system for whatever reason I have never understood.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Only windows atm. Yes I'm reading a book. Deitel and Deitel, Java: How to Program, Tenth Edition, Early Objects. 2014. Based on Java 7 and 8. Was recommended to me by a professor at Maastricht University. I study Cognitive Neuroscience (Research Master) at the University, and since wearables (Google Glass) and basic BCIs still relate on Java, I wanted to learn Java in my vacation break. It's a cool book, but it goes through the basics (not the programming basics, but the Java basics) too quickly. Although it's written for beginners in Java, I would suggest to them that it needs to cover the basic principles of Java in more depth BEFORE starting with syntax. Thank god for you guys ;).

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Quote Originally Posted by gimbal2 View Post
    because the separator character depends on the operating system for whatever reason I have never understood.
    Windows uses the ':' character for the roots of the filing system, e.g. C:\foo\bar; Linux/Unix has only one root so it doesn't need that hack, but a semi-colon can confuse the shell ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Quote Originally Posted by Shredy777 View Post
    It's a cool book, but it goes through the basics (not the programming basics, but the Java basics) too quickly.
    tip: read more slowly?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Don't think it's my reading as I have to read more complex stuff every day. But I will take you suggestion to heart ;).

  17. #17
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Quote Originally Posted by JosAH View Post
    Windows uses the ':' character for the roots of the filing system, e.g. C:\foo\bar; Linux/Unix has only one root so it doesn't need that hack, but a semi-colon can confuse the shell ...
    hm, never thought of it that way. Yep, that makes sense.

    Regarding reading:

    When I was in school I was learning C++. Myself, I wanted to use it for game development and we were only doing Turbo Pascal back then. I bought a book and I read it. During the day I would be playing around with code using stuff I had read about and did not fully understand. And during the evening I would be reading again. And again. And again. Experiment, read. Experiment, read. I didn't care that I did not understand half of what I read, every time I re-read I understood just a little bit more. And eventually I was programming games, creating classes and applying object oriented programming, without schooling. What it took me was about 2 months of insane dedication - that is with prior programming experience. But it is just the way the brain works. You can't cram it in all at once, you need to repeat repeat repeat.

    I think that is something we forget / become bored with when we leave school. If you look at people who come to this forum being "new to Java" (which usually translates to: I am new to programming), they all want to become professionals by reading a book (not happening), it must happen within a timeframe of weeks (not happening) and it all must be automatic with some epic tips and resources on the internet that we all have and must share (but we don't have). The really poor ones come here asking "what library do I use to make my own Facebook".
    Last edited by gimbal2; 08-01-2014 at 09:27 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Mix it with the Oracle tutorials.
    I find having more than one source for these things is handy.

    Also if Deitel and Deitel are suggesting setting the CLASSPATH in the global environment variables then find out where they live and go and visit them. Then hit them over the head with the book.
    If they're just suggesting setting it locally (eg in a BAT file) then apologise...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    Shredy777 is offline Member
    Join Date
    Jul 2014
    Posts
    7
    Rep Power
    0

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    Thanks guys for sharing your personal experience. Very cool community. One thing though, about "that's how the brain works". I find that funny when people say that to me. As I said before, I'm doing a research Master Cognitive Neuroscience at a department that is considered to be the top of bill worldwide. You have no idea how many people live in misconceptions about "how the brain works". About repetition: Yes, it is true that repetition brings the brain to remember things in more detail. But it all comes down to how "flexible" your neural networks are. Some people can read the most difficult-to-understand theories and get it right the first time, where some people need months to grasp even the basics. I'm learning Java because most upcoming wearables rely on Java. Let's say we want to make Google Glass respond to activity in certain frontal lobe neural network activity, we would need a thorough understanding of Java....and probably reprogram the thing from scratch ;). I also first learned C++ and Object Pascal. Somehow, Java to me feels completely different than those other 2 languages. I mean the ideas about OOP are about the same (I'm just starting so please forgive me if I'm wrong), but the way Java approaches it with its syntax and, especially how it interacts with different OS...Still a LOT to learn there for me. I don't know if you guys agree?

  20. #20
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,014
    Rep Power
    6

    Default Re: Java giving me headaches: "Could not find or load main class" error while it is t

    I agree mostly. I don't share your thoughts about the syntax, I felt comfortably at home within the Java language knowing C++ (even more so, since Java is just a lot more aimed at preventing making mistakes). What is wickedly different is the runtime environment; a safe, powerful, reliable and somewhat standardized JVM rather than "the machine".

    And yes every brain is different, I should slap myself on the wrist for generalizing. MOST people need repetition, not ALL ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 06-12-2014, 03:12 AM
  2. "Cannot find main class" error when creating a .jar!
    By davetheant in forum New To Java
    Replies: 17
    Last Post: 11-10-2011, 12:20 PM
  3. Replies: 1
    Last Post: 11-06-2011, 12:26 AM
  4. "could not find or load main class"
    By questionortwo in forum New To Java
    Replies: 4
    Last Post: 09-30-2011, 02:27 AM
  5. Replies: 1
    Last Post: 06-30-2009, 02:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •