java 用classpath编译java没有找到所有的jar文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13618420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiling java with classpath doesn't find all jar files
提问by CodeGuy
I have a lib folder with many .jar files and one of the files I have is mail.jar (for javax.mail)
我有一个包含许多 .jar 文件的 lib 文件夹,其中一个文件是 mail.jar(用于 javax.mail)
When I compile my source code, I use
当我编译我的源代码时,我使用
javac -cp .:../lib/ server/MyFile.java
And then I get a bunch of errors
然后我收到一堆错误
server/MyFile.java:22: error: package javax.mail.internet does not exist
import javax.mail.internet.InternetAddress;
^
server/MyFile.java:23: error: package javax.mail.internet does not exist
import javax.mail.internet.MimeMessage;
^
server/MyFile.java:24: error: package javax.mail does not exist
import javax.mail.Authenticator;
^
etc
etc
However javax.mail should be inside (via mail.jar). I also have activation.jar inside of the lib folder. Note that this mail.jar file I just downloaded today from sun website. It is recent.
但是javax.mail 应该在里面(通过mail.jar)。我在 lib 文件夹中也有 activation.jar。请注意,我今天刚刚从 sun 网站下载了这个 mail.jar 文件。这是最近的。
What could be wrong?
可能有什么问题?
UPDATE:
更新:
I have added all the jar files manually on the classpath, and it compiles fine. However now when I run MyFile.java, using this command
我已经在类路径上手动添加了所有 jar 文件,并且它编译得很好。但是现在当我运行 MyFile.java 时,使用这个命令
java server.MyFile
I get this error:
我收到此错误:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more
回答by trashgod
is there an easier way to do this?
有没有更简单的方法来做到这一点?
Since version 6, you can use class path wildcards.
从版本 6 开始,您可以使用类路径通配符。
javac -cp .:../lib/* server/MyFile.java
See also this related Q&A.
另请参阅此相关问答。
回答by Priya Ranjan Kumar
You can set CLASSPATH=/path/to/java/jar;/path/to/other/jar
(note semicolon) for Unix.
您可以CLASSPATH=/path/to/java/jar;/path/to/other/jar
为 Unix设置(注意分号)。
Also look at http://javarevisited.blogspot.in/2013/02/windows-8-set-path-and-classpath-java-windows-7.html
也看看http://javarevisited.blogspot.in/2013/02/windows-8-set-path-and-classpath-java-windows-7.html