Hi guys, so I started learning Java 2 minutes ago and I have already encountered an error. The error is Error: Could not find or load main class apples.class and I get it after I use the cmd to type java apples.class . Here is the code I compiled to get that apples.class

class apples
    public static void main(String args[])
        System.out.println("Hello youtube!");

So any idea where my mistake is?

Recommended Answers

All 5 Replies

What did you write in the cmd?

well first i wrote javac Test.java to compile it and then i wrote java apples.class to open it

javac.exe needs a file name, but java.exe takes a class name as its parameter, so you don't add the ".class". Ie the correct command is just
java apples
(confusing? yes, but that's how it is)

Actualy its not confusing at all, thank you :)

Just to add up something, this is the case when you create a text file and you compile it using terminal. However, if you have an IDE project where you have the entire dependency tree with folders such as src and bin etc, when you compile a file which is in src/apples.java , you should compile it from the src folder so your command would be javac /apples.java , and then java /apples. Sorry had to mention this incase you get in this situiated because I remember few years ago, it took me ages to figure out what was wrong :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.