I have a java class running as a task (as a plugin in an Java CMS) which used Quartz.
I am now moving it out of the CMS and into a Spring Boot (v1.0.2) project and am using #Scheduled annotation.
The problem i that the ProcessBuilder reports missing executable file, but a File().exist() return true.
It is the same Java version OpenJDK 7, same Application Container (Tomcat 7), on FreeBSD 9.2.
The testing code is a follow:
package test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
#Service
public class SchedulerTest {
#Scheduled(fixedDelay=1800000, initialDelay=5000)
public void test() {
if ((new File("/usr/local/bin/exiftool")).exists()) {
System.out.println("Found exiftool");
}
else {
System.out.println("Did not find exiftool");
}
List<String> args = new ArrayList<String>();
args.add("/usr/local/bin/exiftool");
args.add("-n");
args.add("-S");
args.add("-DateTimeOriginal");
args.add("/image/TEST.JPG");
Process proc = null;
try {
proc = new ProcessBuilder(args).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (proc != null) {
System.out.println("ExifTool output: " + proc.getOutputStream());
proc.destroy();
}
}
}
The ouput is:
2014-04-28 12:30:49.471 INFO 22473 --- [ost-startStop-7] o.s.boot.SpringApplication : Started application in 10.997 seconds (JVM running for 4022.804)
Found exiftool
java.io.IOException: Cannot run program "/usr/local/bin/exiftool": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at no.hoanglai.service.SchedulerTest.test(SchedulerTest.java:32)
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:606)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:184)
at java.lang.ProcessImpl.start(ProcessImpl.java:130)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022)
... 14 more
What may be the problem?
My guessing is the move from Quartz to #Scheduled.
I have also tried to create cutsom bean and set the thread pool to 2, without any luck.
Maybe a file format problem (not really executable?). E.g. see here "aapt" IOException error=2, No such file or directory" why can't I build my gradle on jenkins?.
Related
I have a base and sub class. The requirement is to invoke sub class' method using reflection. Both programs are below.
base.java
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public class base {
public static void main(String[] args) {
URLClassLoader loader = null;
Class<?> cls;
try {
File file = new File("sub.jar" );
URL[] urls = { file.toURI().toURL() };
loader = new URLClassLoader(urls);
cls = loader.loadClass("sub");
base obj = (base) cls.newInstance();
obj.print();
}
catch(Exception e) {
System.out.println("Exception occured:" + e);
e.printStackTrace();
}
}
public void print() {
System.out.println("In Base class");
}
}
sub.java
public class sub extends base {
public void print() {
System.out.println("In subclass");
}
}
compile both in to jars.
javac base.java;
jar cvf base.jar base.class
javac sub.java;
jar cvf sub.jar sub.class
If I invoke the base.jar as "java -cp", it works fine
java -cp base.jar base
output: "In subclass"
But if I invoke it with "hadoop jar" command, I get
hadoop jar base.jar base
Exception in thread "main" java.lang.NoClassDefFoundError: base
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at base.main(base.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: base
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Any help is greatly appreciated.
I believe your issue is on line 15 where you try to case the loader as your base class. I believe the compiler doesn't know how to handle that. Additionally, I'm not sure why you're trying to run this program with Hadoop since it does not have any of the hadoop implementations or classes
The problem is solved by placing base.jar in hadoop classpath. Run "hadoop classpath" command which lists all classpath dirs. Place the jar in one of these directories.
I'm working on a tow projects , EJB project (where i have all my EJB Remote) and the other project is a simple java application with main method. but when i try to lookup an EJB statless and remote via JNDI , it doesn't work.
Really i used many way but no good results.
this is the code and the consol error .
1) My Ejb project when i clean and install it , it gives me a jar file.
2) I use Jboss EAP6.
3) i have a jndi.properties insid of src/main/ressouces where i have this :
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url = localhost:1099
PersonalFactory.java (EJB of the EJB project)
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.lazrak.common.logging.CommonLogger;
import com.lazrak.remotes.IPersonFactoryRemote;
#Remote(IPersonFactoryRemote.class)
#Stateless
public class PersonFactory implements IPersonFactoryRemote
#Override
public void findMe() {
System.out.println("I'am inside of the method findMe");
}
MyMainClass.java (where i have the main method and it is in the second java project)
All import ...
public class MyMainClass {
public static void main(String[] args) throws Exception {
System.out.println("Main start ");
invokeStatelessBean();
}
private static void invokeStatelessBean(){
Hashtable props = new Hashtable();
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
InitialContext context = null;
try {
context = new InitialContext(props);
} catch (NamingException e) {
System.out.println("Faild to add properties to InitialContext"+e.getMessage());
}
try {
final IPersonFactoryRemote statelessRemoteCalculator = (IPersonFactoryRemote) context
.lookup("ejb:"+moduleName+"/"+PersonFactory.class.getSimpleName();+"/"+IPersonFactoryRemote.class.getName());
} catch (NamingException e) {
System.out.println("EJB Remot doesn't been fined\n"+e.getMessage());
}
}
}
when i run my main application , it gives me that :
Main start
enter 2014-09-26 15:57:26,098 DEBUG org.jboss.logging - InitialContextFactory.java:64 - Logging Provider: org.jboss.logging.Log4jLoggerProvider
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/remoting3/spi/ConnectionProviderFactory
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:99)
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:99)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at RemoteEJBClient.invokeStatelessBean(RemoteEJBClient.java:42)
at RemoteEJBClient.main(RemoteEJBClient.java:18)
Caused by: java.lang.ClassNotFoundException:
org.jboss.remoting3.spi.ConnectionProviderFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 11 more
props.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
props.put(Context.PROVIDER_URL,"jnp://localhost:1099");
Those properties are used for connecting to an older jboss version.. For eap 6, you have to use remoting instead of jnp.
HereĀ“s a tutorial that will show you how to connect
http://www.mastertheboss.com/jboss-server/jboss-as-7/jboss-as-7-remote-ejb-client-tutorial
I am following the example listed in this link to add custom email notifications.
In my maven project 'RegistryService': I created the EmailTransformHandler and used maven to create the RegistryService-0.1.jar. I also modified the GREG_HOME/repository/conf/axis2/axis2.xml by adding the handler:
<handler name="EmailTransformHandler"
class="com.registration.example.service.EmailTransformHandler"/>
and dropped the RegistryService-0.1.jar in GREG_HOME/repository/components/dropins
package com.registration.example.service;
import java.util.ArrayList;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
public class EmailTransformHandler extends AbstractHandler implements Handler {
private String name;
public String getName() {
return name;
}
#Override
public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
if (msgContext.getTo() != null && msgContext.getTo().getAddress().startsWith("mailto:")) {
try {
SOAPEnvelope env = msgContext.getEnvelope();
AXIOMXPath xPath = new AXIOMXPath("//ns:text");
xPath.addNamespace("ns", "registry_example");
OMElement element = (OMElement) ((ArrayList) xPath.evaluate(env)).get(0);
element.setText(element.getText().replace("--", "This message intercepted by Terminator"));
} catch (Exception e) {
e.printStackTrace();
}
}
return InvocationResponse.CONTINUE;
}
}
When I go to the GREG_HOME/bin and start the server I get following exception:
2013-02-08 12:05:00,952] FATAL {org.wso2.carbon.core.init.CarbonServerManager} - WSO2 Carbon initialization Failed
org.apache.axis2.AxisFault: Exception occured while loading the Axis configuration from GREG_HOME/wso2/wso2greg-4.5.3/repository/conf/axis2/axis2.xml
at org.wso2.carbon.core.CarbonAxisConfigurator.getAxisConfiguration(CarbonAxisConfigurator.java:190)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:64)
at org.wso2.carbon.core.CarbonConfigurationContextFactory.createNewConfigurationContext(CarbonConfigurationContextFactory.java:65)
at org.wso2.carbon.core.init.CarbonServerManager.initializeCarbon(CarbonServerManager.java:398)
at org.wso2.carbon.core.init.CarbonServerManager.removePendingItem(CarbonServerManager.java:290)
at org.wso2.carbon.core.init.PreAxis2ConfigItemListener.bundleChanged(PreAxis2ConfigItemListener.java:118)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:847)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: org.apache.axis2.deployment.DeploymentException: com.registration.example.service.EmailTransformHandler
at org.apache.axis2.deployment.util.Utils.loadHandler(Utils.java:149)
at org.apache.axis2.deployment.AxisConfigBuilder.processPhaseList(AxisConfigBuilder.java:549)
at org.apache.axis2.deployment.AxisConfigBuilder.processPhaseOrders(AxisConfigBuilder.java:584)
at org.apache.axis2.deployment.AxisConfigBuilder.populateConfig(AxisConfigBuilder.java:150)
at org.wso2.carbon.core.CarbonAxisConfigurator.populateAxisConfiguration(CarbonAxisConfigurator.java:308)
at org.wso2.carbon.core.CarbonAxisConfigurator.getAxisConfiguration(CarbonAxisConfigurator.java:188)
... 8 more
Caused by: java.lang.ClassNotFoundException: com.registration.example.service.EmailTransformHandler
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.axis2.util.Loader.loadClass(Loader.java:261)
at org.apache.axis2.util.Loader.loadClass(Loader.java:229)
at org.apache.axis2.deployment.util.Utils.loadHandler(Utils.java:116)
... 13 more
What configuration am I missing? Googling about this error does not yield any relevant results( except some Russian forums which do not help me much). I even tried adding the jar via WSO2 Management Console as an extension, but I still get this exception.
According to the error, your RegistryService-0.1.jar is not set properly to the class path.
If the jar file is not an OSGI bundle that jar file should be drop in to the "GREG_HOME/repository/components/lib" directory.
-Ajith
I wanted to boot up a Jython interpreter inside the plugin, then execute files inside the interpreter.
package com.jakob.jython;
import org.bukkit.plugin.java.JavaPlugin;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;
public class EmbeddedJython extends JavaPlugin {
private static final Logger log = Logger.getLogger("Minecraft");
private PythonInterpreter interpreter;
private PyObject plugin;
public void onEnable() {
log.info("JythonTest enabled");
this.interpreter = new PythonInterpreter();
log.info("Jython interpreter fired up and cooking on gas");
log.info("Loading Python Plugin");
InputStream pythonScript = EmbeddedJython.class.getResourceAsStream("MyPlugin.py");
this.interpreter.execfile(pythonScript, "MyPlugin.py");
log.info("Getting plg method.");
this.plugin = this.interpreter.get("plg");
log.info("Plugin loaded");
log.info(plugin.invoke("onEnable").toString());
}
public void onDisable() {
log.info(plugin.invoke("onDisable").toString());
log.info("Jython disabled");
}
}
In other tests I can get the Jython interpreter up and running strings I hand it but when I use the above snippet I end up with an exception that looks something like this:
11:03:38 [SEVERE] Error occurred while enabling JythonTest v0.12 (Is it up to da
te?): null
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at org.python.core.ParserFacade.adjustForBOM(ParserFacade.java:371)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:298)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:288)
at org.python.core.ParserFacade.parse(ParserFacade.java:183)
at org.python.core.Py.compile_flags(Py.java:1717)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:235
)
at com.jakob.jython.EmbeddedJython.onEnable(EmbeddedJython.java:26)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:125)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
.java:750)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
r.java:253)
at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:132)
at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:110)
at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:218)
at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:205)
at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:145)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:265)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)
java.io.IOException: java.io.IOException: Stream closed
at org.python.core.PyException.fillInStackTrace(PyException.java:70)
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Unknown Source)
at java.lang.RuntimeException.<init>(Unknown Source)
at org.python.core.PyException.<init>(PyException.java:46)
at org.python.core.PyException.<init>(PyException.java:43)
at org.python.core.Py.JavaError(Py.java:481)
at org.python.core.ParserFacade.fixParseError(ParserFacade.java:104)
at org.python.core.ParserFacade.parse(ParserFacade.java:186)
at org.python.core.Py.compile_flags(Py.java:1717)
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:235
)
at com.jakob.jython.EmbeddedJython.onEnable(EmbeddedJython.java:26)
at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:125)
at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
.java:750)
at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
r.java:253)
at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:132)
at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:110)
at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:218)
at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:205)
at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:145)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:265)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)
Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at org.python.core.ParserFacade.adjustForBOM(ParserFacade.java:371)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:298)
at org.python.core.ParserFacade.prepBufReader(ParserFacade.java:288)
at org.python.core.ParserFacade.parse(ParserFacade.java:183)
... 13 more
Could it be I'm using execfile wrong?
Or is it an error or something I'm missing in how InputStream works?
Are you sure that EmbeddedJython.class.getResourceAsStream("MyPlugin.py"); is finding the resource?
If it can't find the resource, getResourceAsStream will return a null, and it is possible that the Jython interpreter will treat that as an empty (closed) stream.
Your application should probably check this, rather than assuming that getResourceAsStream has succeeded.
If I private File pythonFile = new File('MyScirpt.py'); and then hand getResourceAsStream(pythonFile.getAbsolutePath());
That won't work. The getResourceAsStream method looks for resources on the classloader's classpath, and expects a path that can be resolved relative to some entry on the classpath.
You probably want to do this:
private File pythonFile = new File('MyScirpt.py');
InputStream is = new FileInputStream(pythonFile);
and pass the is to the interpreter. You also need to arrange that the stream is always closed when the interpreter has finished ... by whatever mechanism that happens.
I see in the stacktrace, there is a reference to adjustForBOM (BOM = Byte Order Mark).
This could mean that your file MyPlugin.py is stored in a different encoding to other files that are working for you.
Can you try re-saving the file with a different encoding, say UTF-8?
I would like to determine the jar file name from my java code. I found many solutions in the google, but nothing works. Just to see what I tried here is a stackoverflow forum where a bunch of solutions is posted: stackoverflow
I have Mac OS X 10.6.5.
When I type java -version I get this result:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04-307-10M3261)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03-307, mixed mode)
Thank you for your help.
Edit:
I edit my post to answer for the comment.
Some of the solutions gives me "null" value when I want to System.out.println the path and also fails when I want to create an instance of a File.
Other solutions when I ask for the path they don't give something like file:/....., instead they give something like rsch:/ or something like, this I don't know exactly, but it is a 4 character simple word.
Edit 2:
I run an executable jar from the console. And I would like to have this jar file name in the classes which are in the executed jar file.
Edit 3:
The 4 character word is: rsrc:./
Code how I got this:
File file = null;
try {
System.out.println(MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
Edit 4:
I also tried this code:
package core;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class MyClass {
public String getText(String key) {
String path = "" + MyClass.class.getResource("../any.properties");
File file = new File((path).substring(5, path.length()));
Properties props = readProps(file);
return props.getProperty(key);
}
private Properties readProps(File file) {
Properties props = new Properties();
InputStream in = null;
try {
in = new FileInputStream(file);
props.load(in);
in.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return props;
}
public static void main(String[] args) {
System.out.println(new MyClass().getText("anything"));
}
}
With this result:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1937)
at core.PropHandler.getText(MyClass.java:14)
at core.PropHandler.main(MyClass.java:39)
... 5 more
This code perfectly runs in the eclipse, but when I create the runnable jar file I think you can see the problem.
Is this what you want? http://www.uofr.net/~greg/java/get-resource-listing.html
jcomeau#intrepid:/tmp$ cat test.java; javac test.java; jar cvf test.jar test.class; java -cp test.jar test
public class test {
public static void main(String[] args) {
System.out.println(test.class.getResource("test.class"));
}
}
adding: META-INF/ (in=0) (out=0) (stored 0%)
adding: META-INF/MANIFEST.MF (in=56) (out=56) (stored 0%)
adding: test.class (in=845) (out=515) (deflated 39%)
Total:
------
(in = 885) (out = 883) (deflated 0%)
jar:file:/tmp/test.jar!/test.class
For access to resources that works regardless of the presence of a jar file, I always use classname.class.getResourceAsStream(). But the linked document shows how to use JarFile() for the same purpose.
Ok, finally this is the code which resolved my problem:
String sConfigFile = "any.properties";
InputStream in = MyClass.class.getClassLoader().getResourceAsStream(sConfigFile);
if (in == null) {
System.out.println("ugly error handling :D");
}
Properties props = new java.util.Properties();
try {
props.load(in);
} catch (IOException e) {
e.printStackTrace();
}
With this way it founds my property file.