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

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?

Related

Reading and writing files within Jar

I have finally completed a program in Java and I have to upload it.
The problem is that I have to upload also the executable .jar file and not only the eclipse project.
The main functionality of my program consists by reading and writing .xml files (for example one file is used to read and add new users), and the files in the project folder are so located:
-Project Name
src
default package
main and all other classes
file1.xml
file2.xml
So the two .xml files are in the root of the project.
My question is: It is better to save the .xml files in the JAR and then writing and reading them from the executable program or it is better to store them in a folder outside the .JAR and reading and writing them as externally files?
It is a good practice to create a folder like that?:
-ProjectName
file1.xml
file2.xml
project.jar
I read in Stackoverflow a lot of people having my same issue and a lot of people doesnt know how to manage this problem properly.
Thank you in advance for the reply :)
Changing files in JAR-files can have all sorts of problems. That starts with simple things such as what should happen when you want to update your program to the newest version? Usually you'd just swap the jar, but then you loose everything you edited so far. You'd need a process to update inside the jar.
Other problems include that for changing the jar file you need to open it, possibly realign contents and rewrite the index which could conflict with the JVM that is reading the jar at the same time causing odd behaviour. On some systems (windows...) the Jar file might even be locked while the application is running and thus you cannot write to it at all.
I'd suggest that you add "default files" (in case that your files are initially not just empty) to you Jar file that represent the initial state. If the application is started you check if the XML files exist in the some normal writable directory and if they don't just copy the default files to that directory. This allows you to deploy still just a single jar file, but once started the appropriate files will be created.
You may read a XML file located inside the executable Jar but it is not possible to update (write) a XML file located inside that executable Jar file. So the best option would be:-
-ProjectName
file1.xml
file2.xml
project.jar
The jar should be kept read-only, the XML "files" inside the jar should be read using getResource[AsStream] (class path). You can use those resources as templates to create a copy in the user's (or application's) directory/sub-directory. For the user's directory:
System.getProperty("user.home")

Files not accessible in Deployed Java class, but working locally in Web app developed on play 2.2 framework

I have a web application that I developed in Java over Play 2.2 framework.
When I am running the app locally one of my java class which is accessing some files in the project folders run fine.
After I create a jar file and run it, the java code is not able to read those files. The files are in the public folder and I have ensured packaged in the jar.
There are other files which I have referred from HTML and they are accessible but the java classes are not able to access the files.
Please help me solve this issues.
I think you are trying to access a file inside a jar. If I have understood your question. Please correct me if I am wrong
You could use something like this:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileFromJarFile);
If test.txt was in the root of your JAR file, you'd use:
InputStream is = this.getClass().getClassLoader().getResourceAsStream("test.txt");
NOTE : The file and the java class needs to be in same jar file.
The code was all fine in my case, the directory structure at the jar side was where I missed the part.
I had to keep the files/folders in the directory as same as that of the jar as #APaul said, I tried that but it did not work because I was using a batch to run the jar. My files should have been in the folder containing the batch file. My bad!
Anyways Thanks #APaul.

Accessing a file within Java project

I have a Java project which uses a third party application. I have the license file (.lic format) stored in the resources folder. Upon running the Ant script, it will copy this file into the /lib/jar directory as it rolls up the project into a Jar file to use on the server. This is where I will need to access the file when running the system live. Here is how the folder structure looks
MyProject
src
package
AccessingClass.java
resources
File.lic
lib
jar
File.lic (upon copy from Ant)
I am not sure the best way to do this so any suggestions other than how I have been trying will probably be helpful. The 3rd party project has a method in a class like License.setLicense(), which can either take a String to the location or an InputStream of the file.
I have been playing around with feeding it an InputStream, but always get a null value when calling getClass().getResourceAsStream(). Here is everything I have tried:
getClass().getResourceAsStream("../../../lib/jar/File.lic");
getClass().getResourceAsStream("/File.lic");
And as a backup I also tried (for local builds I figure I would try the resource folder):
getClass().getResourceAsStream("../../../resources/File.lic");
getClass().getResourceAsStream("/File.lic");
Is there a better method to perform this action? Or would someone be able to tell me why what I am trying is failing? Thanks ahead of time.
Are you running this code standalone or in IDE env looks like classpath issue. If you are running at command prompt you have to set classpath to lib dir if in ide make sure you resources dir is in classpath.
First, you need to ensure that the JAR is added in your class path.
Below should work.
InputStream inputStream =
getClass().getClassLoader().getResourceAsStream("/resources/File.lic");
Assuming File.lic is placed in root folder of the jar.

Classpath in a Java Web Start application

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.

How can i create executable apple .app file from my java .jar file?

I have created an executable java Swing .jar application. It works fine on Windows. The application hierarchy is :
application.jar
images(Folder) .......... Contains all images the application uses.
libraries(Folder) ....... Contains all external jar libraries the application uses.
bundles(Folder) ......... Contains all bundle files the application uses.
database(Folder) ........ Contains the database files the application uses.
All the above folders exist outside the jar file. Now i am trying to create a Mac executable file (.app) from "application.jar" to run it on Mac so i used the "Jar Bundler" as specified here but when i run the output application.app file nothing happens, nothing runs and i can't even debug it.
I think the main reason is that it can't see the external folders. So is it impossible to create a .app file if the application has external folders ?
And is there a way to debug the .app file to see what's going on ?
Nothing runs, and i can't even debug it.
Diagnostic output from the launch process may be obtained as follows:
$ export JAVA_LAUNCHER_VERBOSE
$ ./YourApplication.app/Contents/MacOS/JavaApplicationStub
There's a related example here.
Most likely the problem is your working directory.
When you run an executable JAR file by double-clicking it, the working directory is the parent directory of the JAR file.
By default, the working directory of an application bundle is its parent directory. If you package the external folders into the application bundle they will be located under $APP_PACKAGE/Contents/Resources.
So the assumption about the working directory that you make for an executable JAR file does not hold for an application bundle.
In order to set the working directory to the resources directory, add
<key>WorkingDirectory</key>
<string>$APP_PACKAGE/Contents/Resources</string>
to the Info.plist file of your bundle.
In case you know nothing about application bundles, please read this document.
This might help: AppBundler by Josh Marinacci
I am not sure about your exact directory hierarchy. But on a Mac with Xcode installed is an application called "Jar Bundler". It exist for exact that purpose you are asking for.
BTW, Mac application use the suffix .app, that is right. But they are not files. Thery are directories.

Categories