简体   繁体   English

使用jar文件中定义的主类/函数

[英]Using a main class / function defined in a jar file

I'm trying to create the following setup in a Java NetBeans project: 我正在尝试在Java NetBeans项目中创建以下设置:

  • Jar with a main class and main function. 具有主要类和主要功能的Jar。
  • Create a project that use this jar and just add their own specifics (ie some classes are going to be missing from the jar and its up for the project to add those classes). 创建一个使用此jar的项目,并仅添加其自己的详细信息(即,jar中将缺少某些类,因此该项目将添加这些类)。

The jar is running OK by itself (although the classes that are eventually going to be removed are still there). jar本身运行正常(尽管最终将要删除的类仍然存在)。

I'm trying to create the project that will "use" the jar. 我正在尝试创建将“使用”罐子的项目。

I did the setup and put the Jar file in the "Libraries" configuration (in the Project Properties). 我进行了设置,并将Jar文件放入“库”配置中(在“项目属性”中)。

After this, the project structure became like this: 此后,项目结构如下所示:

> MyJavaApp
>> Source Packages
>>> <default package>
>> Librares
>>> The-Lib-From-The-Jar
>>>> <default package>
>>>>> MyJar.jar
>>>>>> mypackage
>>>>>>> AppViewer.class

MyJavaApp is the name of the project that uses the Jar. MyJavaApp是使用Jar的项目的名称。 MyJar is the name of the Jar. MyJar是Jar的名称。 The AppViewer.class is the one that is supposed to start everything up (this class is declared as public). AppViewer.class是应该启动所有内容的类(此类声明为public)。

When trying to execute the project, I got this error: 尝试执行项目时,出现此错误:

Project MyJavaApp does not have a main class set. Project MyJavaApp没有主类集。

Then I went back to the project properties, specifically to the "Run" configuration, and defined the following: 然后,我返回到项目属性,特别是“运行”配置,并定义了以下内容:

Main class: mypackage.AppViewer.class 主类:mypackage.AppViewer.class

After doing this, I get the following message when attempting to execute the project: 完成此操作后,在尝试执行项目时收到以下消息:

The class mypackage.AppViewer.class wasn't found in MyJavaApp project. 在MyJavaApp项目中找不到类mypackage.AppViewer.class。

I tried other variants: 我尝试了其他变体:

Main class: mypackage.AppViewer 主类:mypackage.AppViewer

Main class: AppViewer 主类:AppViewer

with the same results. 具有相同的结果。

I'm looking for some tips on how to do the proper configuration for the above mentioned setup. 我正在寻找有关如何为上述设置进行正确配置的一些技巧。

Thanks in advance. 提前致谢。

Its now working. 它现在正在工作。

Before, I had clicked on the dist folder (expecting to expand it in order to select the .jar file), but instead of expanding the folder, NetBeans placed the whole folder under the Libraries branch of the project tree: 以前,我单击了dist文件夹(希望将其展开以选择.jar文件),但是NetBeans并未将其展开,而是将整个文件夹放在了项目树的Libraries分支下:

>> Libraries
>>> The-Path-To-Lib-From-The-Jar
>>>> <default package>
>>>>> MyJar.jar

I went back to the configuration, and instead of clicking on the folder, I wrote the full path to the .jar file, the project structure looks like this: 我返回到配置,而不是单击文件夹,而是将完整的路径写入了.jar文件,项目结构如下所示:

>> Libraries
>>> MyJar.jar
>>>> mypackage
>>>>> AppViewer.class

Note that there are 2 less levels on the tree. 请注意,树上少了2个级别。

After doing this, when I click on AppViewer.class I get the signatures of the methods (which previously were not there) so I suppose the previous method was not actually placing the .jar in the compilation structure. 完成此操作后,当我单击AppViewer.class时,将获得方法的签名(以前不存在该签名),因此我认为先前的方法实际上并未将.jar放入编译结构中。

Thanks. 谢谢。

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

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