我正在創建我的build.xml文件來構建我的jar與ant。ANT創建jar文件,給getResource +編碼帶來問題
除了將外部資源加載爲圖像之外,所有這一切都是完美的。
我想要做的就是了解如何以正確的方式創建jar。其實我的代碼是:
我知道,問題是包含每行: this.getClass()的getResource(「/ IMG/logo150.png」) (我舒爾這一點,因爲我每次測試一個單獨與日誌記錄和調試,問題在於)
但我不明白爲什麼。我嘗試了數百種可能性,很多不同的jar設置,我在這裏嘗試了很多不同的答案,但沒有解決方案。
我使用Eclipse和文件夾結構:
src -> source code
res -> img -> images
res -> config.files.txt
db -> database files
lib -> libs
任何想法?如果我運行該項目,代碼完美工作,但是當我運行ant build(Run as)時,它不起作用。
編輯:噢,最後我可以發佈代碼!
<?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
此行是不是我寫的。但我有編碼問題(在罐子裏我看不到歐元辛博爾)我應該設置什麼樣的價值王?
第三
是否有任何「正確」或「非標準」文件夾中的Java項目結構(Eclipse還)或我跟我的很順利? (也有許多不同的路徑)
無法下載文件,它需要一個帳戶。 – MadProgrammer
包含'ant build'的輸出,至少包含有錯誤信息的部分。 – Jarekczek
沒有錯誤,唯一的問題是getResource無法加載文件。 – Gianmarco