取出jar包中的配置文件|java 程序打包为jar发布后

|

A. jar包里面的代码如何读取jar包中的配置文件

先看代码目录结构:src/weather/QueryWeather.javaweather.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.classweather.xml用下面的方法就可以找到weather.xmlprivate 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>


赞 (0)