classpath不默认为当前目录 [英] classpath does not default to current directory

查看:102
本文介绍了classpath不默认为当前目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中编写并编译了一个简单的Hello World程序,但是当我尝试运行它时,我遇到了错误消息错误:无法找到或加载主类Hello 。它只是在我执行命令 java -cp之后。 Hello 它已成功运行。

I wrote and compiled a simple Hello World program in Java but when I tried to run it I was greeted with the error message Error: Could not find or load main class Hello. It was only after I executed the command java -cp . Hello that it successfully ran.

奇怪的是,当前目录似乎已成为类路径的一部分。脚本

Oddly enough, the current directory already seems to be part of the classpath. The script

class CheckClassPath{
    public static void main(String args[]){
        System.out.println(System.getProperty("java.class.path"));
    }
}

打印,但我必须再次明确设置 classpath 选项。

prints ., though I again have to explicitly set the classpath option.

有没有人知道为什么这发生了什么?我正在使用Ubuntu 16.04和open-JDK 1.8版。

Does anyone have an idea why this is happening? I am using Ubuntu 16.04 and open-JDK version 1.8.

推荐答案

我希望 CLASSPATH 已设置环境变量。运行 echo $ CLASSPATH 查看它的设置。

I expect that a CLASSPATH environment variable has been set. Run echo $CLASSPATH to see what it is set to.

如果你不使用 -jar 选项然后:

If you don't use the -jar option then:


  • 如果有 -cp ,指定类路径

  • 否则,如果在环境中设置 CLASSPATH ,则指定类路径

  • 否则默认类路径为。使用。

  • If there is a -cp, that specifies the classpath
  • Else if CLASSPATH is set in the environment, that specifies the classpath
  • Else a default classpath of "." is used.

这篇关于classpath不默认为当前目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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