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

1. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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 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. ## 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.

3. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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.

5. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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.

7. ## 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 ;-)

8. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

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

Originally Posted by JosAH
Jos (<--- the good cop ;-)
Well today you are ;)

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

Originally Posted by gimbal2
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 ;-)

10. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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.

11. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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.

13. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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. ## Re: Java giving me headaches: "Could not find or load main class" error while it is t

Originally Posted by gimbal2
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

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

Originally Posted by Shredy777
It's a cool book, but it goes through the basics (not the programming basics, but the Java basics) too quickly.

kind regards,

Jos

16. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

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

Originally Posted by JosAH
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.

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.

18. Moderator
Join Date
Apr 2009
Posts
13,136
Rep Power
23

## 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...:)

19. Member
Join Date
Jul 2014
Posts
7
Rep Power
0

## 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. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## 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 ;)

Page 1 of 2 12 Last

#### Posting Permissions

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