When i compile my java programs (.java) they all compile into .class file. when i try to run the class file with this command : " java C:\Users\User\Desktop\JavaProgram"

It gives the following error: "Error: could not find or load the main class"

how do fix this and how do i compile it then and run it then?

Recommended Answers

All 6 Replies

If you are using a commandline in a console window, you need to be in the correct directory relative to the .class file and use the java command with the classname. If the class is in a package it is more complicated.
For a class not in a package, change to the folder with the .class file and enter:
java <CLASSNAMEHERE>

Also enter the dir command to show what is in the folder.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

It still give the same error even if i am in the same directory

Please post the contents of the console window.
Is there a package statement in the .java file? If so, what is it?

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\User>java C:\Users\Users\t

Thats what i type in to run my java program. it is a .class file
This is the error i get:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\User>java C:\Users\Users\t
Error: Could not find or load main class C:\Users\User\t

C:\Users\User>

There i no packages in my program here is the source code:

    public class t{
        public static void main(String[] args)
        {
            System.out.println("HALLO");
        }
    }

The java command takes only the classname. Nothing more.
java t
You forgot to do the dir command to show what is in the folder where you issued the java command.

oooooo ok well thanks allot man this really helped, can you believe, programming in C++ for almoust a year but cant figure out how to compile a java program. lol

Thanks again man.

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.