0

Im trying to run in a Linux Server with Tomcat a simple java to send emails.

I have the myClass.java and the mail.jar on a directory

I already did:

# javac -cp mail.jar myClass.java

and return no error, but...

when I try to run it like this:

# java myClass

I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/mail/Authenticator
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: myClass. Program will exit.

Its strange, beacuse I run it exactly like that on Windows and it works. I also try to do a simple class with just a System.out inside and it run,

but i dont know why im not getting the Authenticator class, if I already check that it is contain in the mail.jar

Im using java 1.6.0_22

thanks! ^^

4

2 回答 2

0

您包含mail.jar在您的类路径中以编译它,您还应该在运行该类时包含它。

于 2013-01-29T21:40:48.407 回答
0

默认情况下,当您使用java MyClass等命令在命令行中运行 java 程序时,您正在运行 Java SE(标准版)环境。另一个流行的环境是 Java EE(企业版)。

您的程序抱怨的类 (javax.mail.authentication) 来自 Java EE 包,并且在您的运行时绝对不存在

您可以编译代码的事实必须意味着在您的 Eclipse 构建路径(如果您使用 Eclipse)上的某个地方存在一个依赖 jar,而您的运行时中没有包含该依赖 jar(例如:mail.jar)

您需要运行程序以使依赖项在运行时可用,例如:

java -jar /path/to/mail.jar MyClass

或者在 Java EE 环境中运行您的程序

于 2013-01-29T21:45:21.180 回答