2

I am trying to compile and run from the command line a simple java hello world program.

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.

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.

> 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 ., the class is found but not the jar.

I also tried specifying both, but the main class is not found whenever I use a : in the classpath.

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

Mike L'Angelo
  • 854
  • 6
  • 16
mherzl
  • 5,624
  • 6
  • 34
  • 75

1 Answers1

1

In powershell, one solution is to use ; instead of :.

I.e. this works in powershell:

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

The idea to try that came from an answer to this question:

mherzl
  • 5,624
  • 6
  • 34
  • 75
  • 2
    I'm glad you found a solution, but PowerShell is incidental to it, given that both `"./*;."` and `"./*:."` are passed _verbatim_ through to `javac` as `./*;.` and `./*:.`, respectively. The problem sounds like it's related to _platform_ differences: On Unix-like platforms, search paths containing a list of directories, such as contained in the `PATH` environment variable, use `:` as the entry separator, whereas on Windows it is `;` – mklement0 Jan 19 '21 at 21:35