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?

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.

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

Edited 4 Years Ago by NormR1

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");
        }
    }

Edited 4 Years Ago by MasterHacker110

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.

This question has already been answered. Start a new discussion instead.