I have a simple Java program that prints out my classpath. Folder structure is as follows:
[~/tmp/bin]# ls
launcher/ PrintClasspath.class*
And a copy of the same class one more level down in.
[~/tmp/bin/launcher]# ls
PrintClasspath.class*
When I jump up to my ~/tmp directory and run PrintClasspath in my ~/tmp/bin directory, I can run the program just fine, stating ./bin as the classpath.
[~/tmp]# java -cp "./bin" PrintClasspath
/C:/Cygwin/home/user/tmp/bin/
Or I can run the same file I nested in the ~/tmp/bin/launcher directory if I edit the classpath as follows:
[~/tmp]# java -cp "./bin/launcher" PrintClasspath
/C:/Cygwin/home/user/tmp/bin/launcher/
But when I try to sit in my ~/tmp directory, and try to run my class in the ~/tmp/bin/launcher directory with ./bin as my classpath and qualify where the class is located via the following:
[~/tmp]# java -cp "./bin" launcher.PrintClasspath
Error: Could not find or load main class launcher.PrintClasspath
It FAILS. I've run the same test on my Linux box, and qualifying where the class is located in a sub-directory after giving a classpath multiple directories up works fine.
I originally assumed this was a Windows/Cygwin nuance, but I tried the same exercise in Windows command prompt and same result. What am I missing here.do I just have to run my Windows Java programs with a fully qualified classpath?
解决方案
Solved: When setting the classpath in Cygwin using the Windows version of Java, you have to use the cygpath utility with options -wp to convert the unix style paths to Windows paths.
[~/tmp]# java -cp `cygpath -wp ./bin` launcher.PrintClasspath
/C:/Cygwin/home/user/tmp/bin/
(via this)