java - 可执行 jar 不读取 .properties 文件(Build.xml)
问题描述
我已经编写了一个 java 代码,它在 eclipse 上运行良好并读取了所有必要的文件。
该项目包含一个属性文件和一个用作图标的 png 图像。
之后,我在 build.xml 中为它创建了一个构建脚本。
使用 ant 构建工具成功编译构建。
当我执行 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>
.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" );
}
构建.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
解决方案
我认为资源必须在 ${build.dir}/classes 目录中。当您将它们复制到 ${build.dir} 目录时。
在 build.xml 你必须改变
<copy file="${src.dir}/encryption.properties" todir="${build.dir}" />
至
<copy file="${src.dir}/encryption.properties" todir="${build.dir}/classes" />
推荐阅读
- angular - ng2-file-upload 设置了错误的 Content-Type
- c# - NLog不从属性打印对象?
- java - 无论凭据是什么,都在登录页面上循环
- angular - Angular Universal 启用了 initialNavigation 并且 CanActivateChild 返回 false 没有响应
- c# - 集成测试期间的 API 端点错误但不是正常启动
- reactjs - 在 React 中使用 MUI 重置其他组件 onClick 的状态
- linux - 脚本从命令行正确运行,但未嵌入到另一个脚本中
- php - 数据库插入后 HTTP 303 重定向
- excel - 带有输入的 VBA VlookUp 无法获取结果,是代码错误还是数据类型不匹配?
- c# - 如何在 ASP.NET Core 依赖注入中注册具体类的字符串依赖?