无法使用可运行的 jar 加载属性文件
not able to load properties file with runnable jar
我在 Java 中有一个 Maven 项目,其中我在这个目录下有一个 属性 文件 (quartz.properties):
/src/main/resources
现在我可以从这个 class 以两种方式使用这个 属性 文件,如下所示:
/**
* Create a StdSchedulerFactory that has been initialized via
* <code>{@link #initialize(Properties)}</code>.
*
* @see #initialize(Properties)
*/
public StdSchedulerFactory(Properties props) throws SchedulerException {
initialize(props);
}
/**
* Create a StdSchedulerFactory that has been initialized via
* <code>{@link #initialize(String)}</code>.
*
* @see #initialize(String)
*/
public StdSchedulerFactory(String fileName) throws SchedulerException {
initialize(fileName);
}
所以我开始这样使用:
public static void main(String[] args) {
StdSchedulerFactory factory = new StdSchedulerFactory();
try {
factory.initialize(TestClassName.class.getClassLoader().getResourceAsStream("quartz.properties"));
Scheduler scheduler = factory.getScheduler();
scheduler.start();
} catch (SchedulerException ex) {
System.out.println("error= " + ExceptionUtils.getStackTrace(ex));
}
}
这在我的 windows 笔记本电脑上工作正常,没有任何问题,但是当我制作 运行nable jar(导出 --> 运行nable jar --> 将所需的库打包到生成 JAR),然后如果我 运行 在我的另一台 ubuntu 机器上这样:
java -jar abc.jar
我遇到了这个异常:
error= org.quartz.SchedulerException: Error loading property data from InputStream - InputStream is null.
at org.quartz.impl.StdSchedulerFactory.initialize(StdSchedulerFactory.java:576)
at com.example.quartz.TestClassName.main(TestClassName.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
我做错了什么?
更新:- 来自 jar tvf abc.jar
的输出。我只是展示相关的东西,不是全部。
13 Thu Sep 10 18:16:30 GMT-07:00 2015 resources/build.properties
594 Thu Sep 10 18:16:30 GMT-07:00 2015 resources/quartz.properties
1254 Thu Sep 10 18:16:30 GMT-07:00 2015 resources/quartz_config.xml
首先你应该确保jar文件中有quartz.properties。如果不存在,您需要检查 pom.xml
中的构建设置
您的文件在 jar 文件中作为 resources/quartz.properties
,而不仅仅是 quartz.properties
- 所以这就是您需要加载它的方式:
factory.initialize(
TestClassName.class.getClassLoader().getResourceAsStream("resources/quartz.properties"));
或者,以不同的方式创建 jar 文件,以便 quartz.properties
位于 jar 文件的 "root" 目录中。 (考虑到您描述的文件系统结构,这正是我所期望的。)
如果您想从 class 访问 resources/build.properties
,您需要使用绝对路径,如:
TestClassName.class.getClassLoader().getResourceAsStream("/resources/quartz.properties")
我在 Java 中有一个 Maven 项目,其中我在这个目录下有一个 属性 文件 (quartz.properties):
/src/main/resources
现在我可以从这个 class 以两种方式使用这个 属性 文件,如下所示:
/**
* Create a StdSchedulerFactory that has been initialized via
* <code>{@link #initialize(Properties)}</code>.
*
* @see #initialize(Properties)
*/
public StdSchedulerFactory(Properties props) throws SchedulerException {
initialize(props);
}
/**
* Create a StdSchedulerFactory that has been initialized via
* <code>{@link #initialize(String)}</code>.
*
* @see #initialize(String)
*/
public StdSchedulerFactory(String fileName) throws SchedulerException {
initialize(fileName);
}
所以我开始这样使用:
public static void main(String[] args) {
StdSchedulerFactory factory = new StdSchedulerFactory();
try {
factory.initialize(TestClassName.class.getClassLoader().getResourceAsStream("quartz.properties"));
Scheduler scheduler = factory.getScheduler();
scheduler.start();
} catch (SchedulerException ex) {
System.out.println("error= " + ExceptionUtils.getStackTrace(ex));
}
}
这在我的 windows 笔记本电脑上工作正常,没有任何问题,但是当我制作 运行nable jar(导出 --> 运行nable jar --> 将所需的库打包到生成 JAR),然后如果我 运行 在我的另一台 ubuntu 机器上这样:
java -jar abc.jar
我遇到了这个异常:
error= org.quartz.SchedulerException: Error loading property data from InputStream - InputStream is null.
at org.quartz.impl.StdSchedulerFactory.initialize(StdSchedulerFactory.java:576)
at com.example.quartz.TestClassName.main(TestClassName.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
我做错了什么?
更新:- 来自 jar tvf abc.jar
的输出。我只是展示相关的东西,不是全部。
13 Thu Sep 10 18:16:30 GMT-07:00 2015 resources/build.properties
594 Thu Sep 10 18:16:30 GMT-07:00 2015 resources/quartz.properties
1254 Thu Sep 10 18:16:30 GMT-07:00 2015 resources/quartz_config.xml
首先你应该确保jar文件中有quartz.properties。如果不存在,您需要检查 pom.xml
中的构建设置您的文件在 jar 文件中作为 resources/quartz.properties
,而不仅仅是 quartz.properties
- 所以这就是您需要加载它的方式:
factory.initialize(
TestClassName.class.getClassLoader().getResourceAsStream("resources/quartz.properties"));
或者,以不同的方式创建 jar 文件,以便 quartz.properties
位于 jar 文件的 "root" 目录中。 (考虑到您描述的文件系统结构,这正是我所期望的。)
如果您想从 class 访问 resources/build.properties
,您需要使用绝对路径,如:
TestClassName.class.getClassLoader().getResourceAsStream("/resources/quartz.properties")