简体   繁体   English

执行Java程序而不设置类路径

[英]Execute a java program without setting the classpath

I have a program that resides in a directory that is not set in the path/classpath - and I want to execute it using the -cp option on my console. 我有一个程序驻留在未在path / classpath中设置的目录中-我想在控制台上使用-cp选项执行它。 But I get the NoClassDefFoundException , regardless of what I set as -cp 但是我得到了NoClassDefFoundException ,无论我设置为-cp是什么

- out
 `- de
  `- my
   `- package
    `- MainClass.class

I navigate into the package directory and try to execute the main class using 我导航到package目录,并尝试使用执行主类

MyBook-Pro:out flopes$ java -cp . de.my.package.MainClass

What am I doing wrong here? 我在这里做错了什么?

The classpath must contain 类路径必须包含

  • jar files jar文件
  • directories containing the roots of the package tree. 包含软件包树根的目录。

If your package is de.my.package , the root is de , and the directory containing de must be in the classpath. 如果您的软件包是de.my.package ,则根目录是de ,并且包含de的目录必须在类路径中。

So, if you're in the package directory, the classpath should be set to ../../.. : the out directory is the one that should be in the classpath. 因此,如果您位于package目录中,则应将classpath设置为../../..out目录是应位于classpath中的目录。

Or you should use . 或者您应该使用. as the classpath, but launch your program from the out directory. 作为类路径,但是从out目录启动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM