简体   繁体   English

如何正确地将Java项目导出为可运行的jar?

[英]How can I properly export a java project as runnable jar?

I'm trying to learn RMI API in java. 我正在尝试用Java学习RMI API。 Everything finally seems to be working; 一切似乎终于开始了。 My server is up and running, my client can connect. 我的服务器已启动并且正在运行,我的客户端可以连接。 But when I export the project, from Eclipse, as a runnable jar with the Client as launch configuration, now this jar can't connect to my running server. 但是,当我从Eclipse中将项目导出为可运行的jar,并将客户端作为启动配置时,现在该jar无法连接到我正在运行的服务器。 I get an AccessControlException . 我得到一个AccessControlException

    if (System.getSecurityManager() == null) {
        System.setProperty("java.security.policy", "src/mySecurity");
        System.setSecurityManager(new SecurityManager());
    }

Both the client and server has these as the first lines in their main methods. 客户端和服务器都将它们作为主要方法的第一行。 So I think they can't find the 'mySecurity' file after I've exported the project as a jar? 因此,我认为将项目导出为jar后他们找不到“ mySecurity”文件吗? What can I do? 我能做什么?

EDIT: When I access .png files in my projects, I do this MyClass.class.getResource("/resources/myImage.png") That works fine when exported as a jar. 编辑:当我访问项目中的.png文件时,我执行此MyClass.class.getResource("/resources/myImage.png")作为jar导出时效果很好。 But as I understand it, only .java files are supposed to go in the src folder of my project? 但是据我了解,应该仅将.java文件放入项目的src文件夹中? But whatever is outside the src folder is not included in the jar file. 但是jar文件中不包含src文件夹之外的任何内容。 (note that myImage.png is located in src). (请注意,myImage.png位于src中)。

So currently I export the jar and place it in a folder with a resources folder in it(which I copied from my IDE workspace). 因此,当前我导出jar并将其放置在其中包含资源文件夹的文件夹中(该文件夹是从IDE工作区复制的)。 Is this the way I'm supposed to do it, or can I in fact export my project as a executable jar with access resources on the same level with src? 这是我应该采用的方式吗,或者实际上我可以将我的项目导出为具有与src处于同一级别的访问资源的可执行jar吗?

Run your JVMs with 使用以下命令运行JVM

-Djava.security.debug=access,failure,policy

The policy part shows you exactly what policy files are being loaded. policy部分将确切显示正在加载的策略文件。 The rest helps debug all AccessControlExceptions and their causes, and shows the protection domains that are in effect at each one, and the associated policy file in each case. 其余的内容可帮助调试所有AccessControlExceptions及其原因,并分别显示每个域有效的保护域以及相关的策略文件。

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

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