如何从命令行在 Windows 上运行 .class 文件? [英] How do I run .class files on windows from command line?

查看:73
本文介绍了如何从命令行在 Windows 上运行 .class 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令行运行 .class 文件.当我手动移动到它存储的目录时,它可以工作,但是当我尝试这样的事情时:

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:PeterMichaelLazarusMain

它说它找不到主类.除了制作 .jar 文件(我知道 .jar 是最好的解决方案,但目前不是我正在寻找的解决方案)之外,还有其他解决方案吗?

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

推荐答案

Java 应用程序启动器(又名 java.exe 或简单的 java) 预计最多支持四种不同的方式来指定要启动的内容(取决于您使用的 Java 版本).

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

  1. 指定类名是最基本的方法.请注意,类名文件名不同.

  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

这里我们启动类 mypackage.Main 并使用 -cp 开关指定用于查找类的类路径(类的完整路径mypackage.Main 将是 path/to/classFiles/mypackage/Main.class.

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.

启动 jar 文件.

java -jar myJar.jar

这会将 jar 本身及其 Class-Path 条目中指定的任何内容放在类路径上,并启动通过 Main-Class 条目指示的类.请注意,在这种情况下,您不能指定任何其他类路径条目(它们将被静默忽略).

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).

Java 9 引入了模块,并引入了一种启动特定模块的方法,其方式类似于选项 #2 的工作方式(通过启动该模块专用的主类或通过在其中启动用户指定的类)那个模块):

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

  • Java 11 引入了对 单文件源代码程序,这使得执行适合单个源文件的 Java 程序变得非常容易.它甚至会为您完成编译步骤:

  • 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
    

    这个选项对于第一次使用 Java 很有用,但很快就会达到它的极限,因为它不允许你访问在另一个源文件中定义的类(除非你单独编译它们并将它们放在类路径中,这会破坏此方法的易用性,意味着在这种情况下您可能应该切换回选项 #1).

    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).

    此功能被开发为 JEP 330,并且有时仍被如此称呼.

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

    对于您的特定情况,您将使用选项 #1 并通过使用 -classpath 选项(或其简短形式 -cp):

    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:PeterMichaelLazarus Main
    

    如果您的 Main.java 包含您的全部源代码(并且它在同一目录中),那么您可以使用选项 #4,跳过编译step 直接编译执行:

    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:PeterMichaelLazarusMain.java
    

    这篇关于如何从命令行在 Windows 上运行 .class 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆