gpt4 book ai didi

java - 可执行 jar 不读取 .properties 文件 (Build.xml)

转载 作者:行者123 更新时间:2023-12-01 17:21:08 29 4
gpt4 key购买 nike

我已经编写了一个java代码,它在eclipse上运行良好并读取了所有必要的文件。

该项目包含一个属性文件和一个用作图标的 png 图像。

之后,我在 build.xml 中为其创建了一个构建脚本。

使用 ant 构建工具已成功编译构建。

当我执行 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

关于java - 可执行 jar 不读取 .properties 文件 (Build.xml),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61287387/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com