在 Windows 中从命令行执行 java 程序失败 [英] executing java program from command line in windows fails

查看:38
本文介绍了在 Windows 中从命令行执行 java 程序失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 Windows 的命令行运行一个非常简单的单类Hello World"程序.

I'm trying to run a very simple one class "Hello World" program from the command line in Windows.

.java 文件位于C:UsersUserNameDesktopdirecName".包是deem,类名是test.

The .java file is located at "C:UsersUserNameDesktopdirecName". The package is deem and the class name is test.

我可以 cd 到目录并使用 javac test.java 从那里编译它,并且工作得非常好.但是,当我尝试使用以下方法运行它时:

I can cd to the directory and compile it from there using javac test.java and that works perfectly fine. However, when I try to run it using:

java testjava -classpath directory testjava -cp .测试它抛出异常在线程 main java.lang.NoClassDefFoundError: test (wrong name: deem/test).

java test or java -classpath directory test or java -cp . test it throws "Exception in thread main java.lang.NoClassDefFoundError: test (wrong name: deem/test).

如果我使用 java deem.test 它说:Error, could not find or load main class deem.main

If I use java deem.test it says: Error, could not find or load main class deem.main

如何修复异常并使我的程序运行?

How can I fix the exception and get my program to run?

谢谢

推荐答案

这是从命令行运行 Java 程序时常见初学者错误"的变体.

This is a variation of the "common beginners error" with running Java programs from the command line.

java test 或 java -classpath directory test 或 java -cp .测试它抛出线程主java.lang.NoClassDefFoundError中的异常:测试(错误名称:deem/test).

java test or java -classpath directory test or java -cp . test it throws "Exception in thread main java.lang.NoClassDefFoundError: test (wrong name: deem/test).

JVM 实际上是在告诉你在搜索路径上找到了test.class",但是当它读取类文件时,它的结论是该文件应该已经在./deem/test.class" 或 "directory/deem/test.class" ... 取决于您实际使用的-cp"/-classpath"参数

The JVM is in effect telling you that found "test.class" on the search path, but when it read class file, it concluded that the file should have been at "./deem/test.class" or "directory/deem/test.class" ... depending on which "-cp" / "-classpath" argument you actually used

如果我使用 java deem.test 它说:错误,无法找到或加载主类 deem.main

If I use java deem.test it says: Error, could not find or load main class deem.main

现在告诉你它找不到deem/main.class".

This is now telling you that it cannot find "deem/main.class".

请注意,您已经现在告诉它查找名为deem.main"的类,而不是test"或deem.test".(或者你可能只是在那里抄错了一些东西.)

Note that you have now told it to look for a class called "deem.main" instead of "test" or "deem.test". (Or perhaps you just transcribed something incorrectly there.)

规则非常简单:

  1. 您必须将完全限定的类名作为选项后的第一个参数提供给java.(不是简单的类名.不是.class"文件名.不是入口点方法的名称.)
  2. 您必须指定类路径,以便 java 命令可以
    • 将类名映射到类文件的相对路径名中(例如 foo.bar.baz.MyClass 映射到 foo/bar/baz/MyClass.class)...
    • 然后根据类路径条目之一解析该相对路径;例如如果 . 是类路径,则 ./foo/bar/baz/MyClass.class.
  1. You must give the fully qualified class name to java as the first argument after the options. (Not the simple class name. Not the ".class" file name. Not the name of the entry point method.)
  2. You must specify the classpath such that the java command can
    • map the class name into a relative pathname for the class file (e.g. foo.bar.baz.MyClass maps to foo/bar/baz/MyClass.class) ...
    • and then resolve that relative path against one of the classpath entries; e.g. if . is the classpath, then ./foo/bar/baz/MyClass.class.

所以如果 ...

  1. 你的类的全限定类名是deem.test;即 test 类位于 deem, AND
  2. 包中
  3. 对应的class文件在./deem/test.classAND
  4. 它具有所需的入口点方法,AND
  5. 测试应用程序不依赖于您的其他类或第三方库......
  1. the fully qualified class name of your class is deem.test; i.e. the test class is in the package deem, AND
  2. the corresponding class file is in ./deem/test.class, AND
  3. it has the required entry point method, AND
  4. the test application doesn't depend on other classes of yours or 3rd party libraries ...

然后 java -cp .deem.test 应该可以工作.

这篇关于在 Windows 中从命令行执行 java 程序失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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