Running Java Program on Cygwin - classpath not found

亡梦爱人 提交于 2019-12-24 06:58:26

问题


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?


回答1:


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)




回答2:


This is not an answer. I can not comment yet. I wanted to note that this helped me with a problem where I was running javac -cp foo:bar/none which worked on Mac OS. In cygwin on windows it did not work, giving multiple errors about not finding a project xxxx in bar/none. xxxx was already compiled in bar/none. Using cygpath -wp foo:bar/none solved my problem. Example

javac -cp `cygpath -wp foo:bar/none` -d out/whatever/there src/*.java


来源:https://stackoverflow.com/questions/38488352/running-java-program-on-cygwin-classpath-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!