So, I'm trying to run a compiled class file in command prompt on windows 8 like this-

java -cp C:\Java\MyFirstProgram

However, every time I run it using the -cp option, it brings up a list of several help options, such as defining -cp, -showversion, -X, -splash, etc. I've tried several other ways of typing it as well, however when I don't use the -cp option, it says

Error: Could not find or load main class C:\Java\MyFirstProgram

Other questions on Stack mentioned how I should be using -cp, so I tried that and only the help showed up, which was very.. Unhelpful. Anyone know what I'm doing wrong? And if so, thanks for responding if you do.

Edit: This is the source code

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

So how would I include the "class file," then?

有帮助吗?

解决方案

The correct usage of using java executable is:

java [-options] class [args...]

where -options include the -cp command option.

You haven't included your class file, therefore it displays to you the usage help.


To run your program, go to your Java folder and run your program.

Example:

cd C:\Java
java MyFirstProgram

其他提示

I'm guessing the problem is that you need to specify the .class extension:

java -cp C:\Java\MyFirstProgram.class
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top