简体   繁体   English

不能在NetBeans中导入jar文件类

[英]classes of jar file cannot be imported in netbeans

I created a jar file from a bunch of java files. 我从一堆Java文件创建了一个jar文件。 Folder structure was org/ax/redis . 文件夹结构为org/ax/redis I used the command jar cvf jedis.jar org/* . 我使用了命令jar cvf jedis.jar org/* Then I imported this jar file in my netbeans project. 然后,将这个jar文件导入到我的netbeans项目中。 Then when I tried importing classes from it, by writing import org.ax.redis.* . 然后,当我尝试通过编写import org.ax.redis.*从中导入类时。 However, netbeans shows error that no such package exists. 但是,netbeans显示错误,指出不存在这样的软件包。

Now I opened another jar file of log4j to see how it is from inside. 现在,我打开了另一个log4j的jar文件,以从内部查看它的状态。 Only difference was in manifest file. 唯一的区别是清单文件中。 It had a bunch of directives like Name: org/apache/log4j/ . 它有很多指令,例如Name: org/apache/log4j/ So I created a manifest file for my jar file by including Name: org/ax/redis/ . 因此,我通过包含Name: org/ax/redis/为我的jar文件创建了清单文件。 Used this command to add manifest information in my jar jar cvfm jedis.jar META-INF/manifest.txt org/* . 使用此命令将清单信息添加到我的jar jar cvfm jedis.jar META-INF/manifest.txt org/* Still nothing works. 仍然没有任何效果。 Please help me 请帮我

Even If u don't generate .class file , the package still can be imported and it won't show any compilation error(If U don't specify any particular class) . 即使您不生成.class文件,该包仍然可以导入,并且不会显示任何编译错误(如果您未指定任何特定的类)。 However when U try to use a particular class function of that package , it will throw an error . 但是,当U尝试使用该程序包的特定类函数时,它将引发错误。

Jar files typically contain class files (the results of compilation) rather than source files (*.java). Jar文件通常包含类文件(编译结果),而不是文件(* .java)。 While some jar files may contain both, only the class files are available to compile or run against (eg using -cp library.jar ). 虽然某些jar文件可能包含这两个文件,但是只有类文件才可以编译或运行(例如,使用-cp library.jar )。

So basically, before you build your jar file, you need to compile your code - and then include the class files in the jar file. 因此,基本上,在构建jar文件之前,需要编译代码-然后将类文件包含在jar文件中。 If you include the source files as well (in the same directory structure) then some IDEs may be able to detect that, which can be useful. 如果还包括源文件(在同一目录结构中),则某些IDE可能能够检测到该文件,这很有用。

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

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