开发者

How do I run .class files on windows from command line?

I'm trying to run .class file from command line. It works when I manually move to the directory it's stored in, but when I try something like this:

java C:\Peter\Michael\Lazarus\Main

it says it can't find the main class. Is there any solution to this other than making a .jar file (I know that .j开发者_如何学JAVAar is the best solution, but at this moment isn't the one I'm looking for)?


The Java application launcher (a.k.a java.exe or simply java) supports up to four different ways to specify what to launch (depending on which Java version you use).

  1. Specifying a class name is the most basic way. Note that the class name is different from the file name.

     java -cp path/to/classFiles/ mypackage.Main
    

    Here we start the class mypackage.Main and use the -cp switch to specify the classpath which is used to find the class (the full path to the class mypackage.Main will be path/to/classFiles/mypackage/Main.class.

  2. Starting a jar file.

    java -jar myJar.jar
    

    This puts the jar itself and anything specified on its Class-Path entry on the class path and starts the class indicated via the Main-Class entry. Note that in this case you can not specify any additional class path entries (they will be silently ignored).

  3. Java 9 introduced modules and with that it introduce a way to launch a specific module in a way similar to how option #2 works (either by starting that modules dedicated main class or by starting a user-specified class within that module):

    java --module my.module
    
  4. Java 11 introduces support for Single-File Source Code Programs, which makes it very easy to execute Java programs that fit into a single source file. It even does the compile step for you:

    java MyMain.java
    

    This option can be useful for experimenting with Java for the first time, but quickly reaches its limits as it will not allow you to access classes that are defined in another source file (unless you compile them separately and put them on the classpath, which defeats the ease of use of this method and means you should probably switch back to option #1 in that case).

    This feature was developed as JEP 330 and is still sometimes referred to as such.

For your specific case you'd use option #1 and tell java where to look for that class by using the -classpath option (or its short form -cp):

java -classpath C:\Peter\Michael\Lazarus\ Main

If your Main.java contains the entirety of your source code (and it is in the same directory), then you can use option #4, skip the compile step and directly compile-and-execute it:

java c:\Peter\Michael\Lazarus\Main.java


Assuming that Main.class does not have a package declaration:

java -cp C:\Peter\Michael\Lazarus\  Main

Java looks for classes in a "classpath", which can be set on the command line via the -cp option.


I just had the same issue, I tried running java hello.class, this is wrong.

The command should be java hello.

Do not include the file extension. It is looking for a class file, and will add the name on its own.

So running 'java hello.class' will tell it to go looking for 'hello.class.class' file.


Try this:

java -cp C:\Peter\Michael\Lazarus Main

You need to define the classpath.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜