導航:首頁 > 文件管理 > 取出jar包中的配置文件

取出jar包中的配置文件

發布時間:2023-09-08 22:52:55

A. jar包裡面的代碼如何讀取jar包中的配置文件

先看代碼目錄結構: src/weather/ QueryWeather.java weather.xml 程序裡面可以直接讀取到weather.xml文件,代碼如下: private static String getXmlContent()throws IOException { FileReader f = new FileReader("src/weather/weather.xml"); BufferedReader fb = new BufferedReader(f); StringBuffer sb = new StringBuffer(""); String s = ""; while((s = fb.readLine()) != null) { sb = sb.append(s);}return sb.toString();}但是一旦把這個class文件和xml文件打成jar包再運行,對不起,報錯,QueryWeather.class位元組碼根本找不到weather.xml 在看打成jar包的結構:META-INFMANIFEST.MFweatherQueryWeather.class weather.xml 用下面的方法就可以找到weather.xml private static String getXmlContent()throws IOException { Reader f = new InputStreamReader(QueryWeather.class.getClass().getResourceAsStream("/weather/weather.xml")); BufferedReader fb = new BufferedReader(f); StringBuffer sb = new StringBuffer(""); String s = "";

B. java 程序打包為jar發布後,讀取配置文件路徑出錯 ,怎樣獲取配置文件路徑

給你個例子,讀取config.properties文件。
文件內容(值自己加)如下:
TestHosts =
FormalHosts =

TestConfig =
FormalConfig =

HostsPath =
ConfigPath =

讀取文件的類如下:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class EvnConfig{
public static Properties PROPERTIES = new Properties();

static{
String proFilePath = System.getProperty("user.dir")+"/config.properties";
//System.out.println(proFilePath);
//InputStream propertiesStream = EvnConfig.class.getClassLoader().getResourceAsStream(proFilePath);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(proFilePath));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
PROPERTIES.load(in);
}catch(IOException e){
System.out.println("properties創建失敗!");
e.printStackTrace();
}
//System.out.println("EvnConfig.testHosts:"+PROPERTIES.getProperty("TestHosts"));
}

public static final String testHosts = changeCode(PROPERTIES.getProperty("TestHosts"));
public static final String formalHosts = changeCode(PROPERTIES.getProperty("FormalHosts"));
public static final String testConfig = changeCode(PROPERTIES.getProperty("TestConfig"));
public static final String formalConfig = changeCode(PROPERTIES.getProperty("FormalConfig"));
public static final String hostsPath = changeCode(PROPERTIES.getProperty("HostsPath"));
public static final String configPath = changeCode(PROPERTIES.getProperty("ConfigPath"));

public static String changeCode(String str){
String toStr = "";
try {
//System.out.println(str + "轉換...");
toStr = new String(str.getBytes("ISO-8859-1"),"GB2312");
//System.out.println(str + "轉換成功!");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println(str + "轉換失敗!");
e.printStackTrace();
}
return toStr;

}
}

C. jar包中的類讀取配置文件的路徑問題,求大家幫幫忙

應該是直接就可以讀取的。

因為你的配置文件和Jar文件在同一個目錄下,如果你的應用能找到jar文件,也就是可以找到配置文件。不過你要在classpath路徑中添加*.properties。

D. maven把java項目打包,如何把配置文件提出來

你好:這個把配置文件提取出來可以再pm.xml裡面配置java項目文件路徑下文件打包方式來實現。舉例如下,參考下。

<?xmlversion="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.tmtpt</groupId>
<artifactId>tmrpt</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>tmrpt-client</artifactId>
<version>2.2.2</version>
<name>vpetl-client</name>
<url>www.tmrpt.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>tmrpt-client</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<!--<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version><configuration><source>1.6</source><target>1.6</target>
<encoding>UTF-8</encoding></configuration></plugin>-->

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.vprisk.main.EtlClientStart</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${basedir}/target/classes</classesDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<outputDirectory>${basedir}/target/maven-archiver</outputDirectory>
<excludes>
<exclude>*.conf</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<!--拷貝依賴的jar包到lib目錄-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>-dependencies</id>
<phase>package</phase>
<goals>
<goal>-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/maven-archiver/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>-resources</id>
<phase>package</phase>
<goals>
<goal>-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/maven-archiver/resources</outputDirectory>
<resources>
<resource>
<directory>${basedir}/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>${basedir}/src/assembly.xml</descriptor>
</descriptors>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix/>
<mainClass>com.vprisk.main.EtlserverStart</mainClass>
</manifest>

<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>bindtothepackagingphase
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
</project>
閱讀全文

與取出jar包中的配置文件相關的資料

熱點內容
蘋果932攔截騷擾電話 瀏覽:765
盲盒開箱app有哪些 瀏覽:422
win10激活腳本之家 瀏覽:191
魔鬼作坊工具包 瀏覽:185
ae源文件下載 瀏覽:520
如何將照片內容轉換成pdf文件 瀏覽:137
浙里辦app如何更換手機號碼 瀏覽:244
電子資料文件有哪些 瀏覽:241
猥瑣貓表情教程 瀏覽:599
android音頻文件格式 瀏覽:458
漫畫臉app哪裡可以下載 瀏覽:959
購買歡樂升級歡樂豆 瀏覽:282
學習智能機器人用什麼編程最好 瀏覽:655
蘋果手機如何管控app 瀏覽:633
mn文件夾 瀏覽:590
安卓平板通用刷機包下載 瀏覽:751
安卓獲取內部存儲路徑 瀏覽:880
寫代碼兩台顯示器 瀏覽:327
unitypackage壓縮文件 瀏覽:493
奕心安卓 瀏覽:563

友情鏈接