Java 从添加到项目中的jar读取动态web项目中的属性文件
我正在处理一个动态web项目(DWP),它有一个属性文件。我已经创建了一个jar,它应该读取DWP中属性文件的内容和属性文件中的更改,并将更改合并到DWP中。我无法从jar中读取属性文件。我想知道DWP中的jar如何读取DWP的属性文件 这是jar的代码Java 从添加到项目中的jar读取动态web项目中的属性文件,java,jar,Java,Jar,我正在处理一个动态web项目(DWP),它有一个属性文件。我已经创建了一个jar,它应该读取DWP中属性文件的内容和属性文件中的更改,并将更改合并到DWP中。我无法从jar中读取属性文件。我想知道DWP中的jar如何读取DWP的属性文件 这是jar的代码 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; imp
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class fileReader extends Thread
{
publicstatic long oldTMS;
long currentTMS;
File file;
public static long limit;
//static String Path = "./configuration/RequestLimit.properties";
static String Path="configuration/RequestLimit.properties";
public static Properties properties;
public fileReader() throws IOException, InterruptedException {
System.out.println("Reading the property file for the first time");
Thread thread = new Thread();
setDaemon(true);
FileInputStream in = new FileInputStream(Path);
System.out.println("fd:"+in.getFD());
properties = new Properties();
properties.load(in);
String requestLimit = properties.getProperty("RequestLimit");
System.out
.println("request limit as read from the property file is ----"
+ requestLimit);
limit = Long.parseLong(properties.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---" + limit);
start();
// waits for the thread to die
join();
}
public void run() {
System.out.println(" thread starts");
if (Thread.currentThread().isDaemon()) {
while (true) {
try {
Thread.sleep(3000);
System.out.println("entered in infinite loop");
file = new File(Path);
System.out.println("file name is --------" + file);
currentTMS = file.lastModified();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("file initial change time stamp:::::"
+ dateFormat.format(currentTMS));
try {
System.out
.println("lets see if the file has changed or not !!");
FileInputStream d = new FileInputStream(file);
if (oldTMS != currentTMS) {
System.out.println("checking for old and new time stamp!");
oldTMS = currentTMS;
System.out.println("file modified last at-----------"
+ oldTMS);
// reload property
try {
System.out.println("Reloading property file");
properties.load(d);
System.out.println("After Reloading property file properties : "
+ properties
.getProperty("RequestLimit"));
limit = Long.parseLong(properties
.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---"
+ limit);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch (InterruptedException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
}
像这样在DWP中使用jar
package com.ING;
import java.io.IOException;
import com.change.fileReader;
public class deamonCaller
{
public long callDeamon() throws IOException, InterruptedException
{
fileReader fr = new fileReader();
Long requestLimit =fileReader.limit;
return requestLimit;
}
}
将路径提供为-Dproperty=。。。。。到java nad,然后从那里读取它,而不是硬编码值。请使用下面的代码进行尝试。我希望,这会对你有很大帮助
static String Path="configuration/RequestLimit.properties";
loadProperties(Path);
private void loadProperties(String propertiesName) {
if (properties != null) {
return;
}
// Properties properties = null;
InputStream inputStream = null;
inputStream = this.getClass().getResourceAsStream(propertiesName);
if (inputStream == null) {
throw new Exception(propertiesName + "something");
}
properties = new Properties();
try {
properties.load(inputStream);
} catch (IOException e) {
throw new Exception(e);
}
}
对于那些像我一样可能陷入困境的人来说。这对我来说是一个意外的发现。当我向一位大四学生解释这个问题时,一个想法突然闪过我的脑海,付诸实施,并且奏效了。。无论如何,这里有一个解决办法 将Propertyfile直接放在web内容中,无需将文件放在动态web项目的文件夹中。在动态Web项目(DWP)的服务中,使用以下代码行:
String pathPropertyFile = request.getSession().getServletContext()
.getRealPath("")
+ "\\RequestLimit.properties";
System.out.println("path of the property file is"+pathforFileReader);
将此路径作为“pathPropertyFile”传递给包含读取属性文件代码的jar类。
我使用了一个Bean类,在beann中设置路径,然后从Bean中获取路径,并将其传递给读取属性文件的JAR类的方法
public class deamonCaller {
public long callDeamon(Tao tao) throws IOException, InterruptedException {
// obtain the path from the bean Tao
String Path = tao.getPath();
System.out.println("path in deamon:" + Path);
// call the jar method while passing PATH as a parameter
fileReader fr = new fileReader(Path);
Long requestLimit = fileReader.limit;
return requestLimit;
}
}
文件读取器的代码
package com.change;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class fileReader extends Thread {
static long oldTMS;
long currentTMS;
File file;
public static long limit;
//static String Path = "./configuration/RequestLimit.properties";
public static String pathName;
public static Properties properties;
public fileReader(String Path) throws IOException, InterruptedException {
pathName=Path;
System.out.println("Reading the property file for the first time");
Thread thread = new Thread();
setDaemon(true);
FileInputStream in = new FileInputStream(pathName);
System.out.println("fd:"+in.getFD());
properties = new Properties();
properties.load(in);
String requestLimit = properties.getProperty("RequestLimit");
System.out
.println("request limit as read from the property file is ----"
+ requestLimit);
limit = Long.parseLong(properties.getProperty("RequestLimit"));
System.out.println("request limit as per the reading is ---" + limit);
start();
// waits for the thread to die
join();
}
public void run() {
System.out.println(" thread starts");
if (Thread.currentThread().isDaemon()) {
while (true) {
try {
Thread.sleep(3000);
System.out.println("entered in infinite loop");
file = new File(pathName);
System.out.println("file name is --------" + file);
currentTMS = file.lastModified();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("file initial change time stamp:::::"
+ dateFormat.format(currentTMS));
try {
System.out
.println("lets see if the file has changed or not !!");
FileInputStream d = new FileInputStream(file);
if (oldTMS != currentTMS) {
System.out
.println("checking for old and new time stamp!");
oldTMS = currentTMS;
System.out
.println("file modified last at-----------"
+ oldTMS);
// reload property
try {
System.out.println("Reloading property file");
properties.load(d);
System.out
.println("After Reloading property file properties : "
+ properties
.getProperty("RequestLimit"));
limit = Long.parseLong(properties
.getProperty("RequestLimit"));
System.out
.println("request limit as per the reading is ---"
+ limit);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}
}
}
提供你的文件夹结构,你的属性在哪里?我如何在这里上传图片。从未上传图片这是WEB INF中的文件夹配置。该文件夹的属性为filethis,不会出现空指针异常。还有一件事,当我使用“get resource as stream”并更改属性文件时,更改不反映空指针异常??这只是替换你的属性文件读取代码。不是全部覆盖。我这样做了,但它根本没有运行。基本上,我想从jar读取动态web项目中的属性文件和对该属性文件的更改。当我将thsi jar添加到web项目时,系统抛出一个异常:文件未找到jar中的代码工作正常,但当我将其添加到web项目时,jar无法找到属性文件java类路径中的属性文件?