如题 main方法里该怎么写才能读取到放在jar包外面的applicationContext.xml呢?
上图是文件夹内jar包的分布,fileWatcher_lib是项目中运用到的外部包,fileWatcher.jar是自己写的里面Main类是主方法 下图是main方法的开头读取配置文件,试过几个方法 (1)ApplicationContext ac = new ClassPathXmlApplicationContext("C:\Users\q30084th\Desktop\PCtest\applicationContex t.xml");
(2)写一个静态类方法
public class PathUtil {
public static String findXML(){
String filePath=PathUtil.class.getClass().getResource("/").getPath();
filePath=filePath.substring(1).replace("bin", "src");
filePath=filePath.replace("/", "\");
// File file=new File(filePath+"applicationContext.xml");
//测试过 file不为空
return filePath+"applicationContext.xml"; }}
再在main方法里调用
ApplicationContext ac = new ClassPathXmlApplicationContext(PathUtil.findXML());
可是在cmd命令窗口里用 java -jar fileWatcher.jar
都会说找不到配置文件请问各位大大正确的写法要怎么写呢?
新人 没有分...JAR类Spring
上图是文件夹内jar包的分布,fileWatcher_lib是项目中运用到的外部包,fileWatcher.jar是自己写的里面Main类是主方法 下图是main方法的开头读取配置文件,试过几个方法 (1)ApplicationContext ac = new ClassPathXmlApplicationContext("C:\Users\q30084th\Desktop\PCtest\applicationContex t.xml");
(2)写一个静态类方法
public class PathUtil {
public static String findXML(){
String filePath=PathUtil.class.getClass().getResource("/").getPath();
filePath=filePath.substring(1).replace("bin", "src");
filePath=filePath.replace("/", "\");
// File file=new File(filePath+"applicationContext.xml");
//测试过 file不为空
return filePath+"applicationContext.xml"; }}
再在main方法里调用
ApplicationContext ac = new ClassPathXmlApplicationContext(PathUtil.findXML());
可是在cmd命令窗口里用 java -jar fileWatcher.jar
都会说找不到配置文件请问各位大大正确的写法要怎么写呢?
新人 没有分...JAR类Spring
例如:Manifest-Version: 1.0
Class-Path: . test_lib/spring-core-3.2.3.RELEASE.jar test_lib/commons-
logging-1.1.3.jar test_lib/spring-context-3.2.3.RELEASE.jar test_lib/
spring-expression-3.2.3.RELEASE.jar test_lib/spring-beans-3.2.3.RELEA
SE.jar
Main-Class: org.demo.Main另外有一个简单的办法就是:在 Eclipse 里面 -> Export -> Runnable JAR file -> 选择那个 Copy required libraries into a sub-folder ...
filePath+"applicationContext.xml";试试这个
原来的filePath我打印出来看了 没有fileWatch这个字段
你的jar包和applicationContext.xml放在一起,找jar包的路径不就是applicationContext.xml的路径了吗
String filePath=PathUtil.class.getClass().getResource("/").getPath();
filePath=filePath.substring(1).replace("bin", "src");
System.out.println("xml="+filePath);
是C:/Users/administrator/Desktop/PCtest/
用你提供的方法
filePath=filePath.substring(1, filePath.indexOf("fileWatcher"));
这句执行的时候会报错 因为找不到fileWatcher
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class stest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
URL path1= stest.class.getResource("");
String path=path1.toString().substring(path1.toString().indexOf("file"), path1.toString().indexOf("SpringTest"));
ApplicationContext ac=new ClassPathXmlApplicationContext(path+"applicationContext.xml");
}
}
试试这个,我的jdk是1.6。SpringTest是jar包名。我没加log4j
D:\workspace\mywork\SpringTest>java -jar SpringTest.jar
log4j:WARN No appenders could be found for logger (org.springframework.context.s
upport.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
echo %classpath%