我已经编写了一个Java代码,并且可以在Eclipse上正常工作并读取所有必需的文件。
该项目包含一个属性文件和一个用作图标的png图像。
之后,我在build.xml中为其创建了一个构建脚本。
使用蚂蚁构建工具成功编译了该构建。
执行jar时,它不会加载我的图像和属性文件。
Build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="EncryptionUtility" default="deploy" basedir=".">
<property environment="env" />
<property file="build.properties"/>
<path id="classpath">
<fileset dir="${lib.path.all}">
<include name="*.jar" />
<include name="*.zip" />
</fileset>
<pathelement location="${build.dir}" />
</path>
<property name="build.classpath" refid="classpath" />
<target name="prepare">
<delete includeemptydirs="true">
<fileset dir="${build.dir}" includes="**/*"/>
</delete>
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/classes" />
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" debug="on" includes="**/*.java">
<classpath refid="classpath" />
</javac>
</target>
<target name="jar" depends="compile">
<copy file="${src.dir}/encryption.properties" todir="${build.dir}" />
<copy file="${src.dir}/keyicon.png" todir="${build.dir}" />
<copy file="${src.dir}/key.dat" todir="${build.dir}" />
<copy file="${src.dir}/pass.dat" todir="${build.dir}" />
<jar destfile="${build.dir}/EncryptionUtility.jar">
<manifest>
<attribute name="Class-Path" value="lib/bcprov-jdk16-146.jar" />
<attribute name="Main-Class" value="EncryptionUtility"/>
</manifest>
<fileset dir="${build.dir}/classes">
<include name="**/*.class" />
<include name="encryption.properties" />
</fileset>
</jar>
</target>
<target name="deploy" depends="jar">
<copy file="${build.dir}/EncryptionUtility.jar" todir="${deploy.dir}" />
</target>
<target name="clean">
<delete file="${build.dir}" />
</target>
</project>
。properties文件
keyStorePassword=pass.dat
keyStoreKey=key.dat
#keyStorePassword=C:\\Users\\imjme1\\Desktop\\Work_backup\\FMS\\EPM_FILE_ENCRYPTION\\NIFT_SOLUTION\\files\\keyStorePass.dat
#keyStoreKey=C:\\Users\\imjme1\\Desktop\\Work_backup\\FMS\\EPM_FILE_ENCRYPTION\\NIFT_SOLUTION\\files\\keyStoreKey.dat
正在加载图像
// Set the icon of the utility..
ImageIcon icon = new ImageIcon(this.getClass().getResource( "keyicon.png" ) );
Image image = icon.getImage();
Image scaledImage = image.getScaledInstance(120, 120, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
frame.setIconImage( scaledImage );
读取.properties文件
public static String configFile;
public static CommonUtil config;
public static String propertyFileName = "/encryption.properties";
public static Properties properties = new Properties();
// static block to load the property file and assign values from it..
static {
config = new CommonUtil();
try {
config.init(propertyFileName);
} catch (Exception io) {
io.printStackTrace();
}
}
// Initialize the variables for key store credentials and set values from config file..
public void init( String configFileName ) throws Exception {
CommonUtil.configFile = configFileName;
InputStream configFile = CommonUtil.class.getResourceAsStream( configFileName );
properties.load( configFile );
keyStorePass = properties.getProperty( "keyStorePassword" );
keyStoreKey = properties.getProperty( "keyStoreKey" );
}
Build.properties文件
src.dir=C:/Users/imjme1/fms-file-encrytion/RTA_ENCRYPTION_UTILITY/src
build.dir=${basedir}/build
deploy.dir=${basedir}/deploy
lib.path.all=${basedir}/libs
您可以将所有资源文件保存在jar的pwd中。
例如,我正在从build.xml中的build.properties中读取属性,因此我的目录结构将是:
.
myapp.jar
build.xml
build.properties
我认为资源必须位于$ {build.dir} / classes目录中。在将它们复制到$ {build.dir}目录时。在build.xml中,您必须更改
to