I've done a java aplication that have some images as resources like this:
->src
->resources
->images
For accessing this resources i've used getClass().getResource("/resources/images/img.jpg"), this works fine when i am on eclipse, but when i export the project to a jar the path change to something this genre: "jar:C:/path/deployed.jar!/resources/images/img.jpg".
What i am doing wrong? It is possible to export all the project in one jar?
One more question, this resources include a derby db that dont work either when deployed in jar file
Thanks
Try using getResourceAsStream() instead of getResource() to access resources in your jar file.
One more question, this resources include a derby db that dont work either when deployed in jar file
As Hovercraft stated, Derby DB (meaning the data files, not implementation) won't start from a jar. And it doesn't matter you don't insert anything, Derby needs to open these files for writing. They need to be in a directory where you have writing access.
Related
I'm kinda new to spring and web development as a whole.
My question is:
When you build a spring boot project (using Maven) into jar file and deploy it via Docker, everything is in one jar file. How can you access your resources (css, js, images, html...) if you want to edit something? Like change something in css file or add something to html page. Is it even possible? Or do you have to build a new jar file everytime, when you need to change something (in frontend)? Also, when there are being uploaded some images or other files, where are they stored? This stuff is very confusing for me and i can't find any related books or help at all.
Thanks for help!
when you package any java program it is nothing but a zip file. Based on what kind of package it is, you wither name it as a Jar or War.
Jar == Java archive
War == Web archive
Now, given the fact that jar and war both are essentially a zip archive, it gives you flexibility to extract and modify them just like any other zip file.
On windows, I think softwares like 7zip let you update the jar inline. I have done it multiple times, especially when I wanted to change application.properties alone on cloud machines, and no other code changes were required. In such cases, building the whole jar and transferring it again to cloud machine could be time consuming. So I would just extract the contents, update whatever I want to, and rezip the package.
Here is the commands you can use -
jar xf jar-file
This should extract the files into a directory.
This SO thread will guide you towards creating jar files.
Something like jar cf myJar.jar ** should be enough to generate a jar file IMO, but syntax might vary.
The jar file is actually just a zip file containing all the files and classes of your application, so technically you can change files in it like any other zip archive. Best practice is to build the jar file using Maven or Gradle from source every time you need something changed.
It's good practice to keep the source in version control using Git, and tag each build in the git repository - that way you can easily keep track of changes to the jar file by looking at what's in git at the time of the build.
I have been looking for a while now but haven't found exactly what I want. So, I have a project written in Java and have created a small Derby database that I have embedded in the project. Now when creating an executable jar of the project, I cannot access the database anymore unless I put the derby database files in the same directory of where the executable jar is located. However, I do not want this, I really want that the database files are INSIDE the jar.
I have tried to put the database files in a package and reference it like that, without any luck. Could someone point out how I can get this to work please?
Thanks!
EDIT
The goal here is that I want to create more than one executable JAR file and upload them to a Sun Grid Engine and execute each jar with different parameters. However, they all need to be able to access the database and until now I get an error when 1 application already booted the derby database, another application cannot access it. Therefor ALL jar files need to have their own database.
Do you use the jdbc:derby:jar protocol to access your database?
According to the documentation what you're after should be possible.
Inside your app (the one going into the executable jar) you must access the database using the jdbc:derby:jar protocol.
Create the database with the desired content.
When you have built your jar, you can use jar commands to add the database dir to your jar.
All of this could go in your build script, obviously..
There may be issues related to jar sealing and such, YMMV.
I'm trying to make an servlets application with java and oracle10g and I've had it well so far until I need some specific values from some the database, for wich I have a DAO class that handles the connection for retrieving data. I have the following issue.
First off, I excecute a main() method in this class that is suposed to retrieve all entries in some table an print the name of each one in console. I works perfectly.
then I want to return an ArrayList of all those names in order to use them in the servlet. So I make a method just like the one in the main() with the only difference that instead of printing the names, I add each one to an ArrayList which is returned after closing the conection. Well, It gets ClassNotFoundException in the line Class.forName("oracle.jdbc.driver.OracleDriver")
If it helps, I'm guided with this tutorial to connect java applications to oracle databases.
Any help would be appreciated
Put ojdbc14.jar inside your war file at WEB-INF/lib/ directory. You can use ANT task to do this. If you are unfamiliar with ANT, you can just copy the jar file inside WEB-INF/lib/ directory under your project and just zip it using Windows explorer or WinZip or anything else that works for you. Then rename the .zip file to .war and deploy on Tomcat server.
If you want some quick fix just copy the ojdbc jar file to server/lib directory under tomcat and restart tomcat. It should work.
EDIT: refer the comment below. While personally I have not seen any unexpected behavior with JDBC drivers in web-app classloader, but, it is recommended to keep driver jars under Server lib.
I wish to compile my java project in eclipse but I am at a loss and I can't figure out how to include my pictures and my database in the project or how to compile them all into one .jar file. Any help will be appreciated.
To add to what Bruno said. If you're using eclipse to build/package your project then:
add resources subfolder to project root folder
right click project -> properties -> java build path and in source tab add the folder you just created.
You can load data from InputStreams obtained from the classloader (using data on the classpath, possibly in a jar) instead of a FileInputStream (assuming that's what you do) as described here: http://download.oracle.com/javase/1.5.0/docs/guide/lang/resources.html
For the database, it will depend on whether the database engine you're using can load from the classpath. This method doesn't allow you to write, and some database engines may require write access for locking (depending on what you do with the DB).
Any file/directory you copy under the /src of your eclipse project will be included in the jar file created by eclipse and will be accessible from the java programs of that project as classpath resources.
In my project when i clicked the jar file it extracts embedded database files to near it self.
Is there any way to make them stay in the jar file?
You can package a read-only Derby database into one of the jar files in your classpath, and access it using a different form of the connection URL. See: http://db.apache.org/derby/docs/10.8/devguide/cdevdvlp24155.html and http://db.apache.org/derby/docs/10.8/devguide/cdevdeploy11201.html#cdevdeploy11201
Sure it's possible, you can put your DB image into any JAR's classpath and access it using:
Class.getResourceAsStream()