FileNotFoundException with getResourceAsStream() in JAR file - java

I don't know what to do anymore. When trying to execute my program from a runnable .jar file, I get a FileNotFoundException when trying to read my .properties file with getResourceAsStream().
I use Maven 3.5.0 and Java 8 Update 144, Eclipse Oxygen.
I also use the maven-assembly-plugin and the assembly descriptor jar-with-dependencies for producing my single .jar file.
The properties file is located in /src/main/resources (in Eclipse) as it is the convention with Maven. It is then - when building the .jar file - automatically put into the root of the .jar file.
Works perfectly when running the program in Eclipse. I just provide the name of the properties file, not other slashes (/) or anything, for the getResourceAsStream() method. I looked into the .jar file and see that the file is located directly in the root of the .jar file.
Why is the .properties file not found? I just don't get it.
Update 2017-10-12
I posted an answer and thought it would work, but it just seemed to work because I changed the version string and appended a "-SNAPSHOT", but since then, my submodule does not get updated.
I understood that I just have to append the literal "-SNAPSHOT" in order for Maven to recompile the current code that I have changed in Eclipse and put it into my .jar file. But it doesn't.
Maven begins to really annoy me. It is supposed to be working like this. What the heck is now wrong?
And by the way, "mvn clean -U package" that is supposed to force an update does not update the snapshot either. I use the maven assembly plugin.

You need to put a slash at the beginning of your path.
this.getResourceAsStream('/myFile.properties')

Found the solution. I didn't use SNAPSHOT builds in Maven. Turns out I need to use a "[version-number]-SNAPSHOT" for the tag so that Maven always uses the current most-up-to-date code for the build.
Now it works. :)
Update 2017-10-12
OK, I think I know what went wrong. I need to run "mvn install" from my parent directory in order to invoke the compilation and installation of submodule A which submodule B is referencing as a dependency. Then I can run "mvn package" on submodule B in order to get the up-to-date code of submodule A built into the single .jar file. :)
Finally ...

Related

How to create jar file in intellij

I am using intellij community version . I am creating a jar file via build artifact but I am getting file size 67357KB . Which is very big .How to create proper jar file .
Thanks in advance
A "proper" JAR file can be very big. So you are probably creating it the right way.
So what you really need to do is to examine the JAR file to see what the build has put into it. If there is stuff in the JAR that shouldn't be there, you then need to look at what your Maven POM file, Gradle file, or whatever to figure out why the build puts the unwanted stuff in there.
If you are not using Maven, Gradle or some other build system, check the artifact configuration that you have created to specify what goes in the JAR, and what is in the directories you have told it to include.

How to modify a file contained in a JAR that is a dependency in IntelliJ

I am working with Scala in Intellij-Idea, and have run into a problem. I have included the Java tag in case someone with Java experience can also answer my question.
In my .ivy2/cache folder for a certain dependency I am working with, there are two jar files. One contains .class files and one contains .scala source files. I want to be able to modify one of the jars, drop it in my lib folder, then run my program with the jar that includes my changes.
I have an issue though. I can modify the file in the source folder with jar uvf, but when I drop it in lib, and run it, the changes don't show up. I have tried commenting out the dependency in my build.sbt file, but it appears to still be using my old dependency even after I run sbt update.
My question can be divided into several parts:
1) Can I just drop a jar file containing only .scala files in the lib folder and expect it to run?
2) To remove the dependency, is commenting out the line in my build.sbt file and then running sbt update enough?
3) Let us say that the only way to run the jar file as a dependency is to use a jar file containing .class files. How do I rebuild the jar with .class files using the .scala files if I don't have any of the other project files, like the build.sbt file? I have the source files, but I am not sure how to use it to recreate the jar without all the other information that usually comes packaged in a Scala project.
I use Intellij only to edit my files, and use sbt to run the code.
1) Can I just drop a jar file containing only .scala files in the lib folder and expect it to run?
That's not possible because the jar file is expected to contain class files, scala files will be handled as resources instead of source files.
2) To remove the dependency, is commenting out the line in my build.sbt file and then running sbt update enough?
I would run sbt clean instead, I haven't tried but sbt "show unmanagedBase" is supposed to list you the library if it was actually picked.
3) Let us say that the only way to run the jar file as a dependency is to use a jar file containing .class files. How do I rebuild the jar with .class files using the .scala files if I don't have any of the other project files, like the build.sbt file? I have the source files, but I am not sure how to use it to recreate the jar without all the other information that usually comes packaged in a Scala project.
The ideal way would be to find the complete repository for that source, update the source and run sbt publishLocal, if that's not possible, another way would be to reconstruct the build.sbt and keep adding libraries until the library compiles properly, hopefully, it will have no dependencies, or a few only.

Eclipse is showing me the jar, but I'm unable to use the classes in it

I'm building an existing project with Maven in Eclipse and all the dependencies are downloaded. But some of my classes using some classes from a specific jar are not found : the import statement shows an error.
The strange thing is that when I browse the Maven dependencies in Eclipse, I see the jar. I'm also able to unfold the jar, and browse in the packages, and see the classes in it. So as far as I can say, the jar is here in my classpath, available to my project. But somehow, it's not seen by my code. I've tried rebuilding, from Eclipse and from mvn command line, but I still have the same issue.
Any idea where that issue could come from ?
Thanks
One root cause would be that your jar is corrupted. Not corrupted enough to make it unavailable in Eclipse, but corrupted enough to prevent Eclipse from using the classes inside.
To make sure of this :
go to your local Maven repo, where the jar is supposed to be.
try to open the jar with your zip tool (Winzip or equivalent)
if you get an error saying your jar is corrupted, then all you have to do is delete the directory containing the corrupted jar, and build again your project with Maven : jar file would be downloaded again from your remote Maven repository.
If this time you're able to open it, then probably Eclipse will also, and you'll be able to use the classes inside and your problem will be solved.
If you're still unable to open it, check if the jar is not corrupted on central repository itself.
Had you add the jar file to your Build Path, if not, then you can not find the classes in the jar file.
Try to do this:
In Eclipse:
Right click on the jar file --> Build Path --> Add to Build Path.
Then try again if you can find the classes in the jar file or not.

Project is missing required library [Eclipse]

I have imported a java project into a new eclipse workspace and it gave me a lot of errors:
It says "Project 'myProject' is missing required library: /User/linus/.m2/..." (The directory is longer but not of interest for you)
Now, I have looked inside my Finder to see what is in ".m2" but there is no such directory where it should be. Additionally I let it show all the hidden files but no success. Lastly I tried to get into the folder with the terminal (using cd and then the directory eclipse gave me) but that did not work either.
I saw this post but it did not help me.
I reinstalled Maven to make sure it can be used, this is the output if I type mvn --version and mvn in the terminal:
Does anyone have an idea?
Thanks in advance
EDIT:
I reinstalled Maven and now there is a /.m2 folder. It contains /.m2/repository/ but there is nothing in it...
May I suggest you read a Maven tutorial like http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
IMHO it's a wonderful build tool because it gets required jar files per the specification in the project object model ("pom") file which your Maven-ized project should have, and you don't have to keep track of jars on your own.
Look in your project for pom.xml, you might even post it here.
To check the sanity of the project and maven on your computer (ignoring Eclipse for a moment), be sure you have a good Internet connection, open a Terminal window, change directory ("cd") over to the project directory, and type "mvn install".
This should trigger the usual Maven build cycle. One of the first things it should do is download all the jars as identified in the pom.xml and store them deeply nested within your /Users/linus/.m2/ directory. Then it should compile all your java classes. Eventually it will probably build a jar file and copy it to somewhere.
If all this works, visit http://eclipse.org/m2e/ for directions to add the Maven plugin (m2e) to your Eclipse. Then your Eclipse should understand how to download jars and build the project per the pom.xml file.
Eventually this all should fix the original "Project is missing required library" problem.

Unable to export any of my projects into a .jar file

Everytime I try to export any of the projects from my workspace into a Java jar file, I end up with an error:
JAR creation failed. See details for additional information.
Class files on classpath not found or not accessible for: 'Server Manager/src/me/Zahach/ServerManager/main.java'
And this error is repeated for every class in my project.
I tried reinstalling Eclipse, that didn't work.
I then tried to recreate a project and drag and drop my packages into it, that didn't work either.
What is going on?
The error is caused because of a missing .class file(s) for corresponding .java file(s)
In Eclipse
Goto Project->Clean and Rebuild the Project and then try exporting,
If it doesn't work then make sure .class file exists in the bin folder for main.java file in
your case
Another solution is to ensure that the Classpath is Valid
Also make sure that the permissions in your workspace folders are ok. I ran into this problem after updating Eclipse and then downloading some Maven dependencies.
In the end, what fixed it was doing a sudo chmod 777 -R workspace/MyProject
I have faced similar problem while exporting my java files into a compressed jar file, so I checked the Java Build Path of my project and found that the External jars I was referring to from the project are not present in the designated folder locations. Once I have resolved that, I was able to export my java files into a jar.
You may have any java file that is totally empty. You have that file in project with somethingName.java but inside it is nothing. So no bytecode gets generated for it and it throws an error. You can find that file in error details and delete it and try building jar again. It works :) Thumbs up
I had empty Java file classes causing the error.

Categories