简体   繁体   English

Java 类路径问题“。” 与'./*'

[英]Java classpath issue '.' vs. './*'

I am trying to compile and run from the command line a simple java hello world program.我正在尝试从命令行编译和运行一个简单的 java hello world 程序。

It compiles and runs successfully when I do:当我这样做时,它会成功编译并运行:

> javac HelloWorld.java
> java HelloWorld

However, when I place a jar-dependency in this hello world program and the corresponding jar in the same directory, I run into a classpath issue.但是,当我在这个 hello world 程序中放置 jar 依赖项并将相应的 jar 放在同一目录中时,我遇到了类路径问题。

I can compile with:我可以编译:

> javac -cp ./* HelloWorld.java

But I when I attempt to run while specifying the classpath to the jar, my HelloWorld class is not found.但是当我尝试在指定 jar 的类路径时运行时,找不到我的 HelloWorld class。

> java -cp ./* HelloWorld
Error: Could not find or load main class HelloWorld

When classpath is ./* , the jar is found but not the class, and when classpath is .当 classpath 为./*时,找到 jar 但未找到 class,并且当 classpath 为. , the class is found but not the jar. ,找到了 class,但没有找到 jar。

I also tried specifying both, but the main class is not found whenever I use a : in the classpath.我也尝试同时指定两者,但是每当我在类路径中使用:时,都找不到主要的 class 。

> java -cp "./*:." HelloWorld
Error: Could not find or load main class HelloWorld

How can I specify classpath for java to find both the jar and my class?如何为 java 指定类路径以找到 jar 和我的 class?

In powershell, one solution is to use ;在 powershell 中,一种解决方案是使用; instead of : .而不是:

Ie this works in powershell:即这适用于 powershell:

> java -cp "./*;." HelloWorld
hello, world

The idea to try that came from an answer to this question:尝试的想法来自对这个问题的回答:

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

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