I need to provide a settings file for my program, to which the user should have access to write some of the settings i need.
I created a file under a new directory (called settings) on the root of my application, but i have problem finding it at run time.
I use
File SettingsFile=new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()+"settings/CreateSettings.txt");
When i execute this under eclipse i get
/application/home/dir/target/classes/settings/ZipCreateSettings.txt which is wrong.
If i execute it on terminal using java -jar, i get the correct path,
/application/home/dir/settings/ZipCreateSettings.txt
This would cause me problems cos i need to run the application directly from eclipse and not use the terminal, even though it is going to be executed using the jar when it is up and running.
I cant keep it like that anyway, cos this code might end up in someone else's hands, and they would have no idea what to do with it.
I have also used some other techniques like
new java.io.File("").getAbsolutePath(); but this always gives me the current working directory, so if i execute the jar from /home, i would get /home.
I think the problem might be maven (which i am not familiar with at all) since my code worked with a plain java application some time ago.
Since your file will be located outside your classpath, it is basically outside your application. Your application is not aware of files existing outside it's classpath. So you will need some kind of way to provide the Full/Absolute path to your file. You can't use the classloader in your case.
I suggest you use a system param instead of a hardcoded value. See here
Related
I have a JavaFX app that reads in a configuration file. I'd like the config file to remain outside of the jar to facilitate modification without recompiling. Is there a way to set up netbeans to grab the config file and include it in the installer?
I've found the option to change to icon and that works fine but I haven't been able to discover how to tell it to also include specific external resources.
I've read the information posted here: http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm but I'm still not seeing a way to accomplish this.
I don't think there's a way to do this (though I may be wrong). I needed something similar to this once, and the approach I took was
Package the file in the jar file
At program start-up time, check to see if the file exists in the expected location on the local drive
If it's there, read it, etc
Otherwise, read the contents from the jar and write them to the expected file
This solves the problem of "deploying outside the jar", and it also solves the problem of the user inadvertently deleting the file after deployment, etc.
The way I solved this issue (due to having many dependent .dll's and other type items) was to use netbeans to compile the jar, which has things like images/css/fxml etc. etc. and then use an Inno Script to actually compile and configure the installer, since inno makes it pretty straightforward to include extra resources. I have yet to find a way to do this properly within javafx itself.
I'm working on a Swing application that has to be localized.
For that purpose, I'm using a resourceBundle. It works fine when I run the application from the IDE. But when I use maven to generate the jar, the application starts up normally but the internationalization stops working. Without throwing any exception, I have make a lot of variations to solve that problem; I even have make a class that inherits from java.util.ResourceBundle.Control. The only thing I've achieved to make it work is by placing the properties files outside the package (I place it into the desktop just for testing). I then try to use the same method by specifying the path by new File(".").getAbsolutePath().
Doing that still keeps working from running it from Eclipse, but when I try to generate the jar and execute it, it throws an exception and the application does not start.
If you place your properties file into src/main/java next to the Java code, Maven will probably not pick it up.
The canonical location would be src/main/resources (with the package structure below that duplicated just as it is now in your code).
This likely isn't possible, but I thought I would ask just to be sure. I have a batch file which starts my java app using relative paths. Ie. i have:
Application\start.bat
Application\lib*.jar
My application creates a configuration file in the Application directory. My batch script uses relative paths to point to the lib directory jars. This has worked really well for me because I can move the program wherever I want and it will just run. However now I would like to be able to call the same app from command line as well not just from a shortcut which has the working directory set to Application. The problem is that I want to be able to call my application from any directory on the command line and right now this doesn't work because the working directory will be different.
I know I can always add another parameter to my app for the working directory but then I still have to create a batch script with a hard coded path to my application. Is there any way around this in Java, for example to get the directory that my main java file is in on top of the working directory? Is there a launcher app perhaps?
I can't bundle my app as a jar because it creates configuration files which I want to be in the same directory as the application.
Consider just changing current dir in start.bat:
#cd /d %~dp0
java ...
This would change it to the folder where script is located.
pushd/popd commands can also be used to preserve current dir for calling script if needed.
Alternatively getClass().getProtectionDomain().getCodeSource().getLocation() can be used from java to get path to jar/classes.
There is also path-independent approach with config path system property:
java "-DconfigDir=%~dp0" ...
There is no portable solution that is going to work in all cases. Indeed, the notion of an application directory does not necessarily make sense; e.g. consider applets and applications launched using WebStart.
If the notion of an application or installation directory does make sense, then the simplest way to support it is to have the application's launcher / launch script pass it to the application Java code via an application specific -D property.
So I make a couple of images for my eclipse view plugin. Works perfectly. I wake up the next day and the program now tells me that it can no longer find those files. The files are definitely still in the same place I left them, and I haven't touched the code in 24 hrs. I've been messing with this for an hr now and I don't even know where the start debugging this, as my program was fine yesterday.
Sample.gif is in the "icons" folder of my plugin. I've tried moving it to the src folder, and that doesn't work either.
Error message:
org.eclipse.swt.SWTException: i/o error (java.io.FileNotFoundException: sample.gif (No such file or directory))
Code (when I remove this line and all lines of code that use testImage, the plugin runs again):
Image testImage = new Image(null, "sample.gif");
Anyone have any idea what could possibly be causing this, or what information i can provide for someone to give me a direction as to how to debug?
The problem is most likely to do with where the current directory is. When you use a pathname like "sample.gif", the normal behaviour when opening the file is to look in the current directory. If you run the application with a different current directory, the application will look for the file in a different place.
I'm however confused by the current directory concept
Here are some pages that describe the concept:
http://en.wikipedia.org/wiki/Working_directory
http://www.linfo.org/current_directory.html
where should the pictures go?
If you are only ever going to run your application from within eclipse, then you can put them anywhere ... and make sure that your application's Eclipse launcher configuration sets the current directory to a consistent place. (Your current problem is most likely due to the application launcher using the parent Eclipse's current directory, and THAT may depend on where you were when you launched Eclipse ... see linked pages above.)
If you are going to export the built application in some form so that users can use it outside of Eclipse, then the answer depends on the nature of the application and how it will be installed on the user's computer. (On possible solution is to put the images into the application's JAR file, and locate them via the classpath using the ClassLoader.getResource methods.)
I'm writing an app in Java in Eclipse where I need to get the absolute path to an image I'm using. I decided to use getClass().getResource().getPath(), and it works great when I'm running the app from Eclipse.
When I export the app to a JAR file, however, the image doesn't appear. I put in a print statement to find out what was coming from the call above, and it turns out that in Eclipse it comes back with something like "/some/path/to/image.jpg" and when I run it from the JAR, it comes back with "file:/some/path/to/image.jpg". I know the extra "file:" is what is causing the problem, but if I'm using getPath(), it shouldn't be there. Even weirder is why does it show up from the JAR but not in Eclipse?
What do you need the path for? To construct a FileInputStream with? If all what you want to do is to get an InputStream of the image, then just use Class#getResourceAsStream() instead.