简体   繁体   English

Runnable JAR文件导出

[英]Runnable JAR file export

I have a java project . 我有一个java项目。 I want to export it to runnable jar file. 我想将它导出到runnable jar文件。

I use eclipse to do it. 我用eclipse来做。

But when i run created jar file i receive Error ~ file not found : config\\file.xml (the system cannot find the path specified). 但是当我运行创建的jar文件时,我收到Error ~ file not found : config\\file.xml (系统找不到指定的路径)。

How can I export a folder to run success jar file at any where ? 如何导出文件夹以在任何位置运行成功jar文件?

如果您不需要在jar之外配置您的配置,只需将其包含在那里并使用getClass()。getClassLoader()。getResourceAsStream(“config.xml”)访问它,如果它位于“root”的级别“装在罐子里

Sounds like your application is looking for a configuration file using a relative path in the file system. 听起来您的应用程序正在使用文件系统中的相对路径查找配置文件。 In order to make the jar totally self-sufficient, the code would have to be modified to look for the config file in the classpath, and the file would have to be included in the jar. 为了使jar完全自给自足,必须修改代码以在类路径中查找配置文件,并且该文件必须包含在jar中。

To do this, the code that opens the file must be changed to use Class#getResourceAsStream() instead of using a File object. 为此,必须更改打开文件的代码以使用Class#getResourceAsStream()而不是使用File对象。

I'd suggest you use ant or another build system. 我建议你使用ant或其他构建系统。 Here is a short tutorial on ant: 这是一个关于ant的简短教程:

http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

Its pretty easy to create an jar file using xml in your ant build script. 在ant构建脚本中使用xml创建一个jar文件非常容易。 I have done it many times: 我做了很多次:

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

Others may suggest maven and the like, but in reality they are all good choices. 其他人可能会建议maven等,但实际上它们都是不错的选择。 Exporting from an IDE such as eclipse really isn't a viable long term solution. 从诸如eclipse之类的IDE导出确实不是一个可行的长期解决方案。

如果你的项目目录中有一个config-folder,并且你想要读取一个文件,你必须将这个文件夹复制到jar文件的目录中

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

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