0

I'm a beginner in Java and I created this class:

class test
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}

as test.java, and when I compiled it with this cmd:

javac C:\Users\Aimad\Desktop\test.java

and then:

java C:\Users\Aimad\Desktop\test.class

I received this error :

Could not find or load main class C:\Users\Aimad\Desktop\test.class

5
  • 4
    java -cp . test or use an IDE ;) Commented Oct 16, 2012 at 14:20
  • 3
    This as nothing to see with classpath...this is just because he added the ".class" extension Commented Oct 16, 2012 at 14:23
  • @PeterLawrey I tried the java -cp C:\Users\Aimad\Desktop\test but it doesn't worked Commented Oct 16, 2012 at 14:28
  • 1
    Sorry java -cp C:\Users\Aimad\Desktop test It's much simpler when you run from the directory where you code is. ;) Commented Oct 16, 2012 at 14:34
  • First go to the directory which have your main class using "cd" command.Like if your current directory is C:\ use cd Users\Aimad\Desktop and after that just use java test thats it. Commented Oct 16, 2012 at 14:36

6 Answers 6

2

The problem with what you have written is that you have added ".class". By writing this you have instructed Java.exe to look for a class file called "class" in a directory called "test" underneath the one you are currently in. To make you code work remove the ".class" extension and you need to navigate to the file in the command line first. Additionally, the class that holds your main method must be declared as "public".

Code

public class test
{
    public static void main(String[] args)
    {
        System.out.println("Hello World!");
    }
}

Command Line

cd C:\Users\Aimad\Desktop
java test
Sign up to request clarification or add additional context in comments.

2 Comments

To expand on Chris' answer, it is usual for "real" Java applications to arrange the classes into packages. For example java.util.HashMap is the fully-qualified name of a class called HashMap in the package java.util. When you invoke Java, you're not giving a filename but a classname, it already knows to add .class on the end of whatever you ask for.
The java command expects a class name, not a path or file name. What you write under "Command Line" is wrong.
1

Try ths::

java C:\Users\Aimad\Desktop\test

2 Comments

I tried this but I got this error : Could not find or load main class C:\Users\Aimad\Desktop\test
This answer is wrong. java expects a fully qualified class name, not a path name.
1

The solution is either:

% cd C:\Users\Aimad\Desktop
% java test

or

% java -cp C:\Users\Aimad\Desktop test

Explanation:

  1. The argument to the java command must be the fully qualified name of the class. In your case, since your class is declared in the "default" package, the FQN is "test".

  2. The java command needs to be able the class (i.e. the "test.class" file) via its classpath. The default java classpath is . so the first solution is to change to the directory containing the "test.class" file and so that it can be accessed as ".\test.class". The second solution uses the -cp argument to specify a non-default classpath.

Note that using a pathname to identify the class won't work, either with the ".class" suffix, or with the ".class" suffix removed ... (except as above)

Comments

0

Go to the directory where you .class file is and type the command to run your program.

Comments

0

For execution, You need not to write java C:\Users\Aimad\Desktop\test.class .class files is for machine readable so we need not to call them.

simply add the public class name only such as java C:\Users\Aimad\Desktop\test

read here

Comments

0

$cd C:\Users\Aimad\Desktop $java -cp . test

and make class as public

while running the program no need to specify .class and the path it should be fully qualified class name :)

3 Comments

I tried this but I got this error : Could not find or load main class C:\Users\Aimad\Desktop\test
please make change directory to that position and try.? use cd command........ 1.st cd C:\Users\Aimad\Desktop and then Java test
This answer is wrong. java expects a fully qualified class name, not a path name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.