java可执行文件jar无法读取。属性文件(Build.xml)
我已经编写了一个java代码,它在eclipse上运行良好,并读取了所有必要的文件
该项目包含一个属性文件和一个用作图标的png图像
之后,我在build中为它创建了一个构建脚本。xml
使用ant build工具成功编译了构建
当我执行jar时,它不会加载我的图像和属性文件
构建。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>
。属性文件
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 );
阅读。属性文件
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" );
}
构建。属性文件
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
# 1 楼答案
我认为资源必须在${build.dir}/classes目录中。 将它们复制到${build.dir}目录
内置。xml您必须更改
到
# 2 楼答案
您可以将所有资源文件保存在jar的pwd中
例如,我正在读取构建中的属性。来自构建的属性。xml,所以我的目录结构将是: