Eclipse plugin - file not found exception - java

In my plugin project, I added a new file called "database.properties", when I try to run the plugin I am getting below exception:
java.io.FileNotFoundException: database.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
Here is my project structure:
Also I have included that file in build properties, screenshot is shown here:
I tried cleaning, restarting eclipse, building etc. But still system is unable to find the file. Can anyone help me? what I am missing here?

You have to access the file like that:
Bundle bundle = Platform.getBundle("your_plugn_id");
InputStream is = bundle.getEntry("database.properties").openStream();
And includeit in your build.properties:
bin.includes = META-INF/,\
OSGI-INF/,\
database.properties/,\
.,\
plugin.xml

Related

java.io.FileNotFoundException when accessing json file in jar

I'm using this code File serviceAccountFile = new File(Main.class.getResource("/serviceAccountKey.json").getFile()); to access a json file inside my "resources" folder. It works fine when I'm running it from intellij. But i get this error when running the program from the jar.
java.io.FileNotFoundException: file:\C:\Users\Ashirwada\Documents\IIT\JAVA\POS\target\POS-0.6-jar-with-dependencies.jar!\serviceAccountKey.json (The filename, directory name, or volume label syntax is incorrect)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at ashirwada.pos.Firebase.initializeFirebase(Firebase.java:27)
at ashirwada.pos.Main.init(Main.java:33)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
I have other files like fxml files and jpg inside my resource file. They get detected and runs fine. This json file is the only thing thats giving me problems. I opened the Jar with winrar and the json file is there with the rest of the fxml files and the jpg. I'm using maven to compile my jar with the dependencies i need.
That's because after you package your resource into a jar, the string you get by URL.getFile() would be invalid for the constructor File(String path).Use getResourceAsStream instead may solve your problem.
System cannot resolve file C:\Users\Ashirwada\Documents\IIT\JAVA\POS\target\POS-0.6-jar-with-dependencies.jar!\UI\serviceAccountKey.json due to the file is in jar file
You should extract the jar in to a folder and read json file from there
Or you can read file as a resource
InputStream in = etClass().getResourceAsStream("/UI/serviceAccountKey.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try this
File(Main.class.getClassLoader().getResource("/serviceAccountKey.json").getFile());

FileNotFound Exception when trying to spark submit

I have a spark jar which I am trying to submit to my local spark instance. The jar is packages such that all config files are present in the /resources folder as per the maven structure.
Below is the exception:
java.io.FileNotFoundException: file:/Users/prime/Desktop/TibcoMsgConsumer-1.0-SNAPSHOT.jar!/config.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
One observable thing in the above exception is the ! mark present at the end of the jar: TibcoMsgConsumer-1.0-SNAPSHOT.jar!/
If I remove the spark relevant parts (Java Spark context) from this jar and make it a normal Java Jar, I do not face any issue. What might be wrong here?
I decompiled the class and here is how I load the file:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(this.classLoader.getResource("config.properties").getFile());
private Properties properties = this.propLoader.initProp(this.file);
Here is how I run the job:
/spark-submit --conf spark.shuffle.consolidateFiles=true --verbose --class "ril.bigdata.com.Main" --master local[*] ~/Desktop/TibcoMsgConsumer-1.0-SNAPSHOT.jar
What am I missing?
Whatever comes after ! is inside the jar. It is not looking for config.properties inside resources, it's searching in the root of your JAR.
File file = new File(this.classLoader.getResource("resources/config.properties").getFile());
This should make it work.
Also, JavaDoc for getResource

java.lang.NoClassDefFoundError occures using tess4j without Eclipse

I use tess4j library for my java app. I followed instructions and copied liblept168.dll and libtesseract302.dll into my project root folder. When I run my app with Eclipse, it works normally. But when I export it as runnable jar file, it gives me java.lang.NoClassDefFoundError - UnsatisfiedLinkError: The specified module could not be found. But I am sure, that program see this files, because earlier before I added this files it gave me other error, that files can't be found. I also used to copy this dlls to my jar archive, but it didn't help. I am absolutely sure, that I use right jre too. I just can't understand what can be wrong...
Tesseract1 api = new Tesseract1();
api.setTessVariable("tessedit_char_whitelist", "(),-+0123456789");
//part of code of getting image
String result = api.doOCR(image);
Any ideas?
UPD:
I edited my manifest file as you said and it is:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ jsoup-1.7.3.jar json_simple-1.1.jar Filters.jar ja
i_core.jar jna-4.1.0.jar tess4j.jar jai_codec.jar jxl.jar myjsocksj.j
ar jai_imageio.jar
Class-Path: jai_imageio.jar tess4j.jar
Rsrc-Main-Class: com.slando.MainWindow
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Then there were errors about missing dll files and I copied them into jar. And then the same error began to occure.
Stacktrace:
java.lang.NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.Tesseract1
at com.slando.parser.ImageParser.getNumberFromImage(ImageParser.java:109)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:138)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:144)
at com.slando.parser.PageParser.loadPhone(PageParser.java:202)
at com.slando.parser.PageParser.loadFullInfo(PageParser.java:319)
at com.slando.HttpThread.run(HttpThread.java:58)
at java.lang.Thread.run(Unknown Source)
One more:
java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.Native.open(Native.java:1759)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:260)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Native.register(Native.java:1396)
at com.sun.jna.Native.register(Native.java:1156)
at net.sourceforge.tess4j.TessAPI1.<clinit>(Unknown Source)
at com.slando.parser.ImageParser.getNumberFromImage(ImageParser.java:109)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:138)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:144)
at com.slando.parser.PageParser.loadPhone(PageParser.java:202)
at com.slando.parser.PageParser.loadFullInfo(PageParser.java:319)
at com.slando.HttpThread.run(HttpThread.java:58)
at java.lang.Thread.run(Unknown Source)
Eclipse has the tess4j jar in its classpath, but your system does not. To distribute this project, the tess4j jar needs to be in the classpath in your jar manifest. You can also add the path to this jar to the classpath environment variable in your system settings.
Someone else had the same problem

Packing Ressources into Jar and accessing them

I'm fairly new to Java and I'm having trouble exporting my application into a runnable jar.
My application runs some transformations using Saxon and I want the stylesheets used to be inside the runnable jar.
Right now I simply have a subfolder in my project in eclipse and I'm accessing the stylesheets with that path.
However as soon as I export the project as runnable jar inside that jar all the stylesheets are on the same level as the used libraries (subfolder is nowhere to be found.)
Also the application searches for that subfolder in the cwd and of course can't find it there.
How do I pack the stylesheets and access them properly inside the jar ?
//Update:
I think I'm a little further.
I've created a subfolder under src and put all stylesheets in there. I'm accessing them like so:
xsltexps = comp.compile(new StreamSource(new File(this.getClass().getResource("/stylesheets/" + stylesheets).toURI())));
If i run it in eclipse everything works fine. When I export it as runnable jar and run it i get the following exception:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.(Unknown Source)
I have no idea what that even means.
Okay, so here's what actually worked:
xsltexps = comp.compile(new StreamSource(
getClass().getResourceAsStream("/stylesheets/" + stylesheets)));
Most importang change is the use of getResourceAsStream instead of getResource

Why doesn't a runnable JAR exported from Eclipse work?

I have a project which runs fine from within Eclipse.
But when I export it to a runnable JAR with dependencies packed into it it won't run.
The error, when running the JAR from console is:
EDIT (didnt run it with -jar before):
INFO: Loading XML bean definitions from class path resource [applicationContext-
framework.xml]
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
eption parsing XML document from class path resource [applicationContext-framewo
rk.xml]; nested exception is java.io.FileNotFoundException: class path resource
[applicationContext-framework.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:349)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReade
r.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loa
dBeanDefinitions(AbstractXmlApplicationContext.java:80)
at org.springframework.context.support.AbstractRefreshableApplicationCon
text.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtain
FreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:352)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<i
nit>(ClassPathXmlApplicationContext.java:93)
at com.horstmann.violet.UMLEditorApplication.getApplicationContext(UMLEd
itorApplication.java:111)
at com.horstmann.violet.UMLEditorApplication.<init>(UMLEditorApplication
.java:94)
at com.horstmann.violet.UMLEditorApplication.main(UMLEditorApplication.j
ava:84)
... 5 more
Caused by: java.io.FileNotFoundException: class path resource [applicationContex
t-framework.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPat
hResource.java:143)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBea
nDefinitions(XmlBeanDefinitionReader.java:336)
... 20 more
but the file is in resources/ from the JAR-root.
What am I missing or doing wrong?
Try to click on the option “Package required libraries into generated JAR”.
A JAR file is a regular compressed archive. Make sure that all the required libraries in your MANIFEST.MF file are present within the generated file.
Try to run the JAR from the command line - it will most likely display a useful error message.
There probably are some dependencies that eclipse doesn't know about or which aren't configured correctly within it, and are therefore missing from the JAR.
In general, it's not a good idea to rely on IDE functionality to produce deliverables - it depends on too many hidden factors and can't be automated for a build server. Any project that's beyond the experimental/toy stage should have an automated build via Ant or Maven.
The message
Could not find the main class: violet-0.21.2-SVN.jar. Program will exit.
indicates that you have gotten the Main-Class: MyPackage.MyClass line wrong in the Manifest when packaging together the jar file. You need to give the full name of the class with the main(String[] args) method.
See http://download.oracle.com/javase/tutorial/deployment/jar/appman.html for details.

Categories

Resources