I made a java program with a properties file named config.properties.
It works perfectly on Eclipse.
I'm trying to create an executable jar for this programe.
Using the classic method (right click on project, export, executable jar file...) i get a working jar but when i try to edit my config.properties file the changes are not taken in account for the following execution of my jar.
How can I get, on the one hand an executable jar and on the other hand a config.properties file (outside of my Jar) that can be edited by the users to change the parameters of my Jar code ?
Currently my property file is stocked in /src and declared like this :
public static ResourceBundle bundle = ResourceBundle.getBundle("config");
When I need to use one of the properties of this file in my java code I use :
bundle.getString("Car.Color");
Thanks for your help :)
Edit your classpath to include a directory within which your properties file is located. For example:
java -classpath C:\java\MyClasses com.myapp.RunIt
Related
I have a jar that is reading a file using below code:
Thread.currentThread().getContextClassLoader().getResource(fileName);
I want to run this jar using java -jar .jar command but I want to keep this file outside my jar, so that I can edit the jar file later on without touching the jar. Can anyone help me, how to run this jar so that it will pick up the file from outside.
There are multiple approaches you can do that and it will depend on where would you like to place this external file. For the sake of this answer, I will refer to this file as config file
Not In The Same Directory
The first approach is where you will need to place this file outside the JAR and not necessarily next to the JAR file in the same directory. In that case, you can pass the file location of the config file using either an environment variable (if you are running the JAR in a shell for example) or a Java property.
To use an environment variable, assuming you are using some Linux distro, then you can use the export command to set the value; something like this:
$ export CONFIG_FILE_LOC=/etc/myapp/config.file
You can then read the value in your code using the System class by using the following code:
String fileLocationEnv = System.getenv("CONFIG_FILE_LOC");
Alternatively, you can set this as a property by adding the following segment to your launch command:
$ java -Dconfig.file.location=/etc/myapp/config.file -jar myapp.jar
You can then read the value in your code using the System class for properties using the following code:
String fileLocationProp = System.getProperty("config.file.location");
In The Same Directory
If you need the config file to co-exist in the same directory as your JAR file, then you can use the following code to get the JAR directory and then append the filename to it. Here's the code (assuming a class named MyApp)
try{
new File(MyApp.class.getProtectionDomain().getCodeSource().getLocation().toURI());
} catch(URISyntaxException exception){
System.out.println("Exception");
}
Hope that helps.
To open the file as a resoure, add the folder containing the file(s) you want to use, to your classpath:
java -classpath .;config -jar myjar.jar
This example adds the current directory and the config directory to your classpath.
Multiple folders can be specified by using a separator. On windows use ';' , on unix use ':' .
To open the file as a File, you can just use
new File("configfile")
which will look in the working directory (directory where you launched your java)
I work with NetBeans IDE an I have a .txt file saved in src/myapp folder. If I run from the IDE, this recognise my
File file=new File("src/myapp/mytext.txt");
But if I build the jar file and double click it or launch it from command line I get this error:
java.io.FileNotFoundException: src\myapp\mytext.txt
I could insert absolute path, but how can I run my jar independently by the position of my project in the computer?
You can obtain the file path indepently of its position with the following:
ClassLoader classLoader = getClass().getClassLoader();
String path = classLoader.getResource("mytext.txt").toString();
Java is expecting to find the file relative to your working directory. So by hardcoding the file in src/myapp/mytext.txt you are expecting the user of your application to have the
file under the same folder structure.
If you are expecting the file to be at the same level of your jar file, you can just use ./mytext.txt. Do not put your mytext.txt under the src in your project. That is for sources you want to compile and/or bundle inside your jar file. In NetBeans move it outside the /src folder, that way when you run the program from your IDE or when you run it externally from your Jar file you find the file at the same level.
If you want the user to be able to specify the location himself of the file, you can also read the command line arguments (the arguments to your public static void main(String[] args)).
there is no such problem
File file=new File("./src/myapp/mytext.txt");
My maven project is a standalone java appplication. I need to run this form a Unix box. So i made a runnable jar.But i have to update a date in the app.properties file in every run.I tried maven jar plugin to make a fatty runnable jar with all dependency. It is running fine , but not able to edit the app.properties file
You can read a properties file from the classpath (root package) like this:
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/app.properties")));
You can start a runnable jar with a custom classpath like this:
java -cp app.properties:app.jar my.main.AppClass
(you cannot use java -jar because then the -cp option is ignored)
Put the file app.properties in the same directory as app.jar. The location of app.properties will be the first entry on the classpath and thus the code in the jar can load it as shown above.
If you make sure there is also a copy of app.properties in the jar, then that will be used as default if the external properties file is missing. (note that this only works for the complete properties file, not for individual properties)
I am working on creating an Android app and need to import a jar containing a class that I created into it, the class doesn't contain a main method, so, as far as I understand, the jar won't be executable, which is fine. I just need it to be able to be used within the application.
How do I create the jar file?
You can try creating by using command prompt: jar cvf jar-file-name folder-of-classes
Steps to do:
1) Take all the required `.class` files place it into a folder named `xyz`in directory `D:\abc`
2) Open Command prompt and change the current working directory to D:\abc
3) run command as jar cvf test.jar xyz
check the directory D:\abc there will be a jar file named test.jar containing the folder with all the class files.
Hope it helps
jar file is just a bunch of .class file. select your android package Right click -> New -> Java Class.
copy your old class file code into newly created. Do care class name & .class file be same name.
if you mean to import java .jar dependency
Select your app. Right click goto New->Module->import .jar/.aar package
Using the Jar file in your application
In your applications Android.mk; specify the name of the JAR file under the LOCAL_JAVA_LIBRARIES :=
I have an issue with path names in my code. Let's say I have a main class:
com.test.LoadFile.java
Similarly I have a myxml.xml file under com.test. Meaning that the Java file and XML file are under same package.
Can somebody suggest how, when I do (inside LoadFile)
File file = new File("???/myxml.xml")
What should the path be, to support both:
Eclipse IDE code (after including the above code into a single Java project)
and
Run the main LoadFile class outside of the IDE (in a JAR file)
What should I use as the value of the path variable to include in the generated project JAR?
You can read the XML file using getResourceAsStream(), as long as it's in the CLASSPATH:
InputStream is = LoadFile.class.getClassLoader().getResourceAsStream("/myxml.xml");
EDIT: If you are packaging into a .jar, you must specify the complete path of the resource from the jar's root folder using "/" at the beginning of string