首页 > 解决方案 > 可执行 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

标签: javaant

解决方案


我认为资源必须在 ${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" />

推荐阅读