gpt4 book ai didi

java - ANT 创建 jar 文件,导致 getResource + 编码出现问题

转载 作者:行者123 更新时间:2023-12-02 07:32:10 25 4
gpt4 key购买 nike

我正在创建 build.xml 文件来使用 ant 构建我的 jar。

除了将外部资源作为图像加载之外,一切都很完美。

我想做的就是了解如何以正确的方式创建 jar。实际上我的代码是:

我知道问题出在包含以下内容的每一行:this.getClass().getResource("/img/logo150.png")(我对此很确定,因为我通过日志记录和调试单独测试了每一个,问题就在那里)

但我不明白为什么。我尝试了数百种可能性,jar 的许多不同设置,我在 stackoverflow 中尝试了许多不同的答案,但没有解决方案有效。

我使用的是eclipse,文件夹结构是:

src -> source code
res -> img -> images
res -> config.files.txt
db -> database files
lib -> libs

对此有什么想法吗?如果我运行该项目,代码可以完美工作,但是当我运行 ant 构建(运行方式)时,它不起作用。

编辑:哦,是的,我终于可以发布代码了!

<?xml version="1.0" encoding="UTF-8"?>
<project name="Garby Gestore Clienti" default="try_jar" basedir=".">
<description>
This buildfile is used to build the jar of the program.
</description>

<!-- ================== Property Definitions ===================== -->
<property file="${user.home}/Garby.properties" />
<property file="${user.home}/.Garby.properties" />
<property file="${basedir}/Garby.properties" />

<!-- ================= File and Directory Names ==================== -->
<property name="src" location="${basedir}/src" />
<property name="build" location="${basedir}/build" />
<property name="dist" location="${basedir}/dist" />
<property name="app.name" value="Garby" />
<property name="dist.jarHome" value="${user.home}/Garby" />
<property name="app.version" value="1.0" />
<tstamp />
<property name="jar.name" value="${app.name}_${app.version}.${DSTAMP}.jar" />
<property name="jar.completePath" value="${dist.jarHome}/${jar.name}" />



<!-- Here you must specify the directory in which jar files needed by this plugin to run are stored -->

<property name="shared.lib" value="${basedir}/lib" />

<!-- =============== Custom Ant Task Definitions =================== -->
<property name="compile.debug" value="true" />
<property name="compile.deprecation" value="false" />
<property name="compile.optimize" value="true" />

<!-- ================== External Dependencies ======================= -->
<property name="Database" value="hsqldb.jar" />

<!-- ================== Compilation Classpath ======================= -->

<path id="compile.classpath">
<!--<pathelement location="${src.home}"/>-->
<fileset dir="${src}">
<include name="**/*.java" />
</fileset>
<fileset dir="${shared.lib}">
<include name="**/*.jar" />
</fileset>
</path>

<!-- =================== All Target ================================ -->

<!-- ================== Try_jar Target ============================ -->
<target name="try_jar" depends="compile, dist, signjar, clean_class_files, run" description="Clean build and dist directories, then compile, create and sign jar and finally run Core.jar" />

<!-- ================== Clean Target ============================== -->

<target name="clean" description="Delete old build and dist directories">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>

<!-- ================== Prepare Target ============================= -->

<target name="prepare" depends="clean">
<mkdir dir="${build}" />
<mkdir dir="${build}/classes" />
<mkdir dir="${build}/lib" />
<copy todir="${build}/lib">
<fileset dir="${shared.lib}" includes="${Database}" />
</copy>
</target>

<!-- ================== Compile Target =========================== -->

<target name="compile" depends="prepare" description="Compile Java sources">

<mkdir dir="${build}/classes" />
<javac srcdir="${src}" destdir="${build}/classes" encoding="8859_1" debug="${compile.debug}" deprecation="${compile.deprecation}" optimize="${compile.optimize}" source="1.7" target="1.7">
<classpath refid="compile.classpath" />
</javac>
</target>

<!-- =================== Dist Target ================================ -->
<target name="dist" description="Creates Jar archive">
<!-- Create the time stamp -->
<tstamp>
<format property="compile.timestamp" pattern="yyyyMMddHHmm" />
</tstamp>
<!-- update core version in manifest -->
<replaceregexp file="${basedir}/manifest" match="Implementation-Version: .*" replace="Implementation-Version: ${app.version}.${compile.timestamp}" />
<!-- Create Jar file -->
<jar destfile="${jar.completePath}" manifest="${basedir}/manifest">
<fileset dir="${build}/classes" excludes="**/*.bak" />
<fileset dir="${basedir}/bin"/>
<zipfileset src="${shared.lib}/${Database}" />
</jar>

<copy todir="${dist.jarHome}/db">
<fileset dir="${basedir}/db" includes="**" />
</copy>
<copy todir="${dist.jarHome}/res">
<fileset dir="${basedir}/res" includes="**" />
</copy>


</target>

<!-- ================== SignJar Target ============================ -->
<target name="signjar" description="Signs jar with keystore taken from your shared.lib folder">
<!--<signjar jar="${jar.completePath}" keystore="${shared.lib}/paripari.jks" alias="irc" storepass="${keystore.password}" keypass="${private.password}"/>-->
</target>

<!-- ================== Javadoc Target ============================ -->

<target name="javadoc" depends="compile" description="Create Javadoc API documentation">

<mkdir dir="${dist}/docs/api" />
<javadoc sourcepath="${src}" destdir="${dist}/docs/api" packagenames="*">
<classpath refid="compile.classpath" />
</javadoc>

</target>

<!-- ================== RmBak Target ============================= -->

<target name="rmbak" description="Delete *.bak files everywhere">

<delete>
<fileset dir="${docs}" includes="**/*.bak" />
<fileset dir="${src}" includes="**/*.bak" />
</delete>

</target>

<!-- ================== RmLog Target ============================= -->

<target name="rmlog" description="Delete *.log files from build directory">

<delete>
<fileset dir="${build.home}" includes="**/*.log*" />
</delete>

</target>
<!-- =================== Delete .class Target===================== -->
<target name="clean_class_files" description="Delete .class files stored inside build directory and dist folder">
<delete dir="${build}" />
<delete dir="${dist}" />
</target>

<!-- ================== Run Target =============================== -->
<target name="run" description="Run Garby">
<java jar="${jar.completePath}" fork="true">
</java>
</target>

</project>

第二个问题:

encoding= 8859_1

这行不是我写的。但我在编码方面遇到问题(在 jar 中我看不到欧元符号)我应该设置什么值(value)之王?

第三

Java 项目(也在 Eclipse 中)是否有任何“正确”或“标准”文件夹结构,或者我的项目进展顺利? (还有许多不同的路径)

最佳答案

使用这样的东西:

<copy todir="${yourJarFolder}/folder">
<fileset dir="${basedir}/folder" includes="**" />
</copy>

此代码将工作区中的文件夹直接复制到 jar 的文件夹中,不是在 jar 内,而是同一个文件夹中。

之后,您可以使用标准方法而不是使用 getResource() 来访问任何资源。

希望它对你有用。

关于java - ANT 创建 jar 文件,导致 getResource + 编码出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12766085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com