I have a requirement to dynamically extract the content of a Jar file to a local directory. Remaining part of application will use these content. Everything working well in my eclipse development environment. However following peace of code returns null when it comes into JNLP launch.
InputStream stream = VLCLibManager.class.getClass().getClassLoader().getSystemResourceAsStream("XXX.jar");
I already did following :
Manifest file of JAR that contains VLCLibManager.class updated with proper class-path entries
My XXX.jar is placed under /lib directory of JNLP. It's getting downloaded correctly
Have the entry (jar href="lib/XXX.jar"/>>) in XYZ.jnlp file
Any help appreciated as I'm stucked with this issue for the past few days.
Finally I resolved the issue. Thought of publishing here as it would help others who face similar issue.
I did below :
I packed resources that I wanted as a Zip file and placed into /resources directory of my maven project
Maven compiler plugin packs this zip file along with resultant jar.
So I can load the zip file to my java code using
YourClass.class.getResourceAsStream("/XXXX.zip")
This loads resources to java program. You can unzip as you wanted and use it whereever needed
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 want to put my java Project in a .jar or in an .exe. I can not find a solution for my problem.
That is my project structure:
project
1.1. pom.xml
1.2. .idea
1.2.1. libaries
1.2.1.1. many maven dependency files
1.3. .GUI
1.3.1. .idea
1.3.1.1. libaries
1.3.1.1.1. many maven dependency file
1.3.2. src
1.3.2.1. main
1.3.2.1.1. java
1.3.2.1.1.1. .java files
1.3.2.1.2. recources
1.3.2.1.2.1. .fxml files
1.3.2.1.2.2. CSSStylesheets
1.3.2.1.2.2. .css files
I follow instructions on youtube for creating a .jar in Intellij (https://www.youtube.com/watch?v=ZlyTn8PZ3Fc) but the .jar was not executable. It just print this Display parameters as parsed by Maven (in canonical form) and comparison result: to the console after running java -jar file.jar. (After the double dot there does not come more information.)
Next I found another youtube video that shows me it does not work like in the first video because of the maven dependencies (https://www.youtube.com/watch?v=HGHu-SzL-5E). In this video I do not know what to do in 5:39. He copies something but I do not know from where that is. I tried to put the same files in there like he did but it does not work. There comes a ClassNotFoundError that it can not be found the .fxml files. Then I do not write them in the command but all other .java files but it was the same error.
On this (https://introcs.cs.princeton.edu/java/85application/jar/jar.html) site I had my next try. I follow the instructions like this: I dreated a .mf file and put all .fxml and .java and .class files in the command but it can not be found the .class files so I put them out of the command. And then ist comes a ClassNotFoundError. It can not find the Main class. Intellij mark it in the .mf file red as well and I do not know why.
Then I follow the instructions of this (https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes) site but there an empty .jar file was created.
Now I do not know what I did wrong or what I miss so I hope someone can help me.
What's going on is that I have my images and .wav files in a a project folder and it's running fine in eclipse, however once I export it into a executable jar file it stops loading images. I've tried Extracting libraries into jar file and I've tried packaging them in a jar file. What do I do? I'm panicking because I need to get this ready by tomorrow! I know I have the path right, it's starts the code, but stops after a little.
I get this error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings:
AdventureQuestv4.0.2/src/adventureQuestv/AdventureQuestMain.java
Here's the code I use to load an image
URL GiantT2 = AdventureQuestBetaMain.class.getResource("GiantT.png");
giantT = ImageIO.read(GiantT2);
I know the images are in the file, it's just that they're not loading.
check out this post.May be you are also having the same issue.
How to export jar with images on Eclipse?
Use fat jar eclipse plugin. Select all resources like .wav and image files etc while exporting.
Before exporting resolve all Java compiler errors and warnings(to the maximum extent).
While exporting the jar file with images in your eclipse ,
You must check these in the Jar Export dialog box,
1.Export generated class files and resources
2.Export Java source file and resources
Options:
Compress the content of the jar file
Add directory entries
please delete the existing workspace and reimport the source code
i hope this may resolve issue
In my project i write into and read from a project.txt file. This project.txt file is first reading in by programm start.
All works perfectly, but if i export my project as a .jar file and try to execute this .jar file, then my programm says, that he could not find the project.txt (i created this error-message).
The path of project.txt is "./res/project.txt". The res-folder is included in the build-path of this project. I'm using Eclipse.
What i'm doing wrong?
TO READ
Two possible causes:
you are trying to access the project.txt through java.io and not as resource (please post the code)
you access through ClassLoader.class.getResourceAsStream("/res/project.txt"); but the file was not exported into the jar (please post the code)
in othe words: the "./res/project.txt" is near the eclipse project path but outside the scope of the jar.
TO WRITE
Finally the writing into the jar file is absolutly not Java compliant, it should work in some cases such as ear or war and not jar.
I want to add DLL's, images, textfiles etc to my project as resources so when I export it, the jar contains the resources so they can be used. I am using eclipse.
Problem is I have no idea how to add it. I've tried adding DLLs/pics to the src folder in the project, but when I export the jar, it is not located there
I've looked at How to make a JAR file that includes DLL files? but it only explains how to extract it, not how to add it to the project and build.
EDIT: I am using an applet to open the jar by the way, sorry for missing it!
Cheers
How are you opening the file in java?
Class.getResourceAsStream(name)?
If you are packaging the code in a jar, then you need to use that command. (as opposed to new File(name), which will get the file in the same directory as your jar)
If the file is not physically in your jar, you can check by changing .jar to .zip and extracting it, then check out this doc http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
Usually in an eclipse project, the src folder is the wrong place to put non-sourcecode-content.
You should try moving to maven as your build system, as it is highly customizable and provides you with folders inside your project for exactly that purpose. (src/main/resources)