简体   繁体   English

jar文件中的Java导入类

[英]Java import classes from jar file

I have a Class written which imports some classes from JavaMail. 我有一个编写的类,它从JavaMail导入一些类。 When the application runs, it downloads the file automatically and saves it in a directory called depend. 当应用程序运行时,它会自动下载文件并将其保存在名为depend的目录中。 But I can't get it to import the classes from the Javamail jar I downloaded. 但我无法从我下载的Javamail jar中导入类。 For Example, I have: 例如,我有:

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

At the top of the file, these classes are located in (Path to program/depend/javax.mail.jar) 在文件的顶部,这些类位于(path to programs / depend / javax.mail.jar)

You must ensure that the javamail.jar file is on the classpath . 您必须确保javamail.jar文件位于classpath

You can do this using -classpath arg or by setting the CLASSPATH env var. 您可以使用-classpath arg或通过设置CLASSPATH env var来完成此操作。

  • Here is documentation from Oracle describing java -classpath command line arg 以下是Oracle的文档 ,描述了java -classpath命令行arg
  • Here is documentation describing how to set CLASSPATH environment var. 以下是描述如何设置CLASSPATH环境var的文档

It sounds like you want to load classes over the network. 听起来你想通过网络加载类。

This can only be done using a ClassLoader . 只能使用ClassLoader完成。 It cannot be done any other way . 它不能以任何其他方式完成。

It can be done using RMI - but that uses a ClassLoader internally. 它可以使用RMI完成 - 但在内部使用ClassLoader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM