如何从命令行在 Windows 上运行 .class 文件?

发布于 2024-09-02 05:59:42 字数 202 浏览 8 评论 0原文

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

java C:\Peter\Michael\Lazarus\Main

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

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 .jar is the best solution, but at this moment isn't the one I'm looking for)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木槿暧夏七纪年 2024-09-09 05:59:42

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

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

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

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

  2. 启动 jar 文件。

    java -jar myJar.jar
    

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

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

    java --module my.module
    
  4. Java 9 引入了模块,并引入了一种 >Java 11 引入了对 单文件源代码程序,这使得执行适合单个源文件的 Java 程序变得非常容易。它甚至为您完成编译步骤:

    java MyMain.java
    

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

    此功能被开发为JEP 330,有时仍然这样称呼。< /p>

对于您的具体情况,您可以使用选项 #1 并使用 -classpath 选项(或其缩写形式 -cp)告诉 java 在哪里查找该类):

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

如果您的 Main.java 包含整个源代码(并且位于同一目录中),那么您可以使用选项#4,跳过编译步骤,直接编译执行:

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

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
云雾 2024-09-09 05:59:42

假设Main.class没有包声明:

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

Java在“类路径”中查找类,可以通过-cp选项在命令行上设置。

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.

沫尐诺 2024-09-09 05:59:42

我刚刚遇到了同样的问题,我尝试运行 java hello.class,这是错误的。

命令应该是java hello

请勿包含文件扩展名。它正在寻找类文件,并将自行添加名称。

因此,运行“java hello.class”将告诉它去寻找“hello.class.class”文件。

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.

氛圍 2024-09-09 05:59:42

试试这个:

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

您需要定义类路径。

Try this:

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

You need to define the classpath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文