Classpath in a Java Web Start application - java

I need to load an .xml file in META-INF, it works when the application isn't sandboxed, but in Java Web Start a different classloader (which is more restricted) seems to be used, so the file found in myproj.jar/META-INF/myfile.xml isn't loaded. It however works if I put the file in the current directory of the loading class (I put it under com/blabla/myproj/whatever/META-INF/myfile.xml).
I couldn't find any classpath settings within the .jnlp file, but perhaps this can be done with a manifest? I don't know how they work, so if that's the solution please supply an example.

Stuff in META-INF should not be readable by code in your jar file, since that code should be agnostic to the fact that it's packaged in a JAR. Instead, since it is meta information (meta-inf) about the Jar itself, only the application that loads the jar file should access it.

Related

Running a Java Web Start application without Java Web Start

I want to run a fat client delivered as a Java web start application without Java web start. I launched it via javaws and managed to get all the jar files mentioned in the JNLP file from the cache after they were downloaded.
I tried running the jar file that contains the main class according to the JNLP file, but I get the 'Could not find or load main class' error. Were I just trying to run a class I'd set the classpath accordingly, but since I'm running a jar file with java -jar, as far as I know the classpath settings will be ignored anyway. Now I'm not sure what to do, does anybody know how to tackle this?
I'll answer this myself now, turns out it is stupidly simple: Get all the jar files, unzip them to get the content, merge all the content (best done with rsync), create a new MANIFEST.MF file that contains the main class to be loaded and the merged hashes for all existing files from all MANIFEST.MF files, zip again to create a jar. That's it.

Can i write file located at classpath

I want to write a text file at classpath. want to make some changes in that file.
this is spring boot application and packaging it as a jar. So basically this text file is located in jar & I want to make changes to that file.
Don't know it is possible or not.
but please suggest to me how I can do that?
Files inside a jar can be read from (called resource files in general).
You cannot modify them directly though. For that, you need them to be outside the jar.
Possible duplicate of Updating resource files at runtime
If it is a .properties file though, there are ways to do it.
Following blog seems helpful (https://crunchify.com/java-properties-files-how-to-update-config-properties-file-in-java/)

Properly deploying a small Java project

I'm currently working on a small Java Project (~30 Classes, 5 external libs).
The code accesses resources in the folders src/resources and src/test_resources using getClass.getResouce("/resources/any.file").
Most of these resource files will probably never be touched by a user, but there are also some regular configuration files which are intended to be edited by the end users.
My question now is: How should I be deploying such an application?
Exporting everything into a runnable jar doesn't seem to be a good way, as I don't wanna torture my users and let them unzip the jar for editing the configuration files.
Should I export all of the internal stuff into the jar, and copy the resources directory into a Folder side by side with it? How can I access the resources then?
Thank you guys!
You could copy the resources folder. It doesn't necessarily need to be side-by-side with the jar file. The key is that you need to put the parent folder of the resources folder on the classpath.
For example, you could copy it someplace like:
c:\some\directory\resources
Then, when you execute, do something like:
java -cp c:\some\directory;c:\some\path\to\your.jar;... your.main.ClassName

Jar cannot find the path to the file inside

In my Maven project I have a properties file that has a property for a location of keystore file file=filename.p12 (I think the file type doesn't really matter now).
The problem I have that when i built it with maven, I see that the file is inside the root of jar and when i run java -jar the-jar-file.jar I get the IO exception that the filename.p12 is not found.
Everything runs fine in Eclipse, it finds the file and the application runs. Not to confuse somebody, I keep a copy of that filename.p12 as well in src/main/resources folder so that the paths are resolved running in Eclipse and standalone. But this is going to be my other question.
What I can't do is to get the filename.p12 as a resource, because I have external jar that gets as argument my properties file and then handles that properties file itself where the row file=filename.p12 is. Why is the file not found inside the jar, even though I see it's there? My other property files that I have open with Spring's ClassPathResource run just fine.
In order to access internal/embedded resources you need to use Class#getResource or Class#getResourceAsStream depending on your needs

Application bundle on OS X does not work, file addressing problem

My application requires a XML file to work and it doesn't even start without the file. Why I bundle my app as a JAR file it works fine as long as the XML file is placed in the same directory as the JAR file.
When I'm exporting the project as an OS X application package, the application does not work. If I copy the XML file in the same directory where the application package is, it works.
So I'm pretty sure that it is a minor addressing problem to access the XML file from within my Java code. I'd like to put the XML file into the application package. Simply copying it in the same directory where the JAR file is does not work.
The file, or better the files are addressed like this: doc = sax.build("file.xml"); and are located directly in my project folder.
I'm working with Eclipse and I export my project directly from Eclipse as an application bundle.
I also tried it with the OS X Jar Bundler, which delivers the same result.
So, how do I address my files correctly, so that I can place them into the application bundle?
Any help is appreciated! Thank you very much!
You are most likely reading it in as a physical file, which needs to be located in the current working directory.
Have you considered reading it in as a resource instead which allows it to be found via the classpath?

Categories