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

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

问题描述

我试图在Windows中从命令行运行一个非常简单的类Hello World程序。



.java 文件位于C:\Users\UserName\Desktop\direcName 。该包是 deem ,类名为 test



我可以cd到该目录,并使用 javac test.java 从那里编译,这是完全正常。但是,当我试图运行它:



java测试 java -classpath目录test java -cp。 test
it throws线程中的异常 main java.lang.NoClassDefFoundError:test / test)



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



如何修复异常并让我的程序



感谢

解决方案


java测试或java -classpath目录test或java -cp。测试它throws线程中的异常main java.lang.NoClassDefFoundError:test(错误的名称:deem / test)。


JVM告诉你在搜索路径上找到test.class,但是当它读取类文件时,它断定文件应该在位于./deem/test.class或directory /

使用的-cp/-classpath参数。


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


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



请注意,你现在有现在称为deem.main而不是test或deem.test。



这些规则真的很简单:


  1. 您必须将完全限定类别名称指定为 java 作为选项后面的第一个参数。 (不是简单类名。不是.class文件名。 >
  2. 您必须指定类路径,以便 java 命令可以

    • 映射类名转换为类文件的相对路径名(例如 foo.bar.baz.MyClass 映射到 foo / bar / baz / MyClass.class )...

    • 然后根据其中一个类路径条目解析该相对路径;例如如果是类路径,则 ./ foo / bar / baz / MyClass.class


  3. 类必须具有所需的 public static void main(String [])入口点方法。


  4. 您的类的完全限定类名是 deem.test ;即 test 类位于包 deem AND

  5. 相应的类文件位于 ./ deem/test.class AND

  6. 它具有所需的入口点方法 AND

  7. 测试应用程序不依赖于您的其他类或第三方库...

THEN java -cp。 deem.test 应该可以正常工作。


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

The .java file is located at "C:\Users\UserName\Desktop\direcName". The package is deem and the class name is test.

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 test or java -classpath directory test or java -cp . test it throws "Exception in thread main java.lang.NoClassDefFoundError: test (wrong name: deem/test).

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?

Thanks

解决方案

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

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

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

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

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

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

The rules are really rather simple:

  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.
  3. The class must have the required public static void main(String[]) entry point method.

So IF ...

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

THEN java -cp . deem.test should work.

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

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