Executable .jar fails to start because of .properties file loading - java

I have the problem running executable .jar file. I've created a project which contains a .properties file. It works just fine when I start it from eclipse, but when I export it to executable .jar file and try to run it with:
java -jar myfile.jar
I get the following exception:
(couldn't post image here)
http://imageshack.us/photo/my-images/824/29583616.png/
I've checked my manifest file in the .jar and it contains the
Class-Path: .
And here's the properties file loading:
properties = new Properties();
properties.load(new FileInputStream(
"src/com/resources/treeView.properties"));
Any idea what causes this exception?

If the properties file is inside the jar file, you cannot access it as a file.
You need to ask the classloader to get the resource as an inputstream. See Getting the inputstream from a classpath resource (XML file)

In Eclipse (and in most IDEs) the current directory is the project's root directory. This means that Class-Path: . means something else in Eclipse than when you run it from the command line. This is why you wrote "src/com/...". Remove "src":
properties.load(new FileInputStream("com/resources/treeView.properties"));

Your properties file is within JAR file. So, use : ClassLoader.getResourceAsStream().

Related

prunsrv service with external properties file

I have a java .jar file I created using NetBeans. I am using apaches procrun (prunsrv.exe) to install that .jar as a Windows Service. I modified the code to get a property from a config.properties file.
I added the config.properties file to the same folder that my .jar file resides in.
My code is as follows:
Properties props = new Properties();
InputStream inputStream = MyService.class.getClassLoader().getResourceAsStream("config.properties");
props.load(inputStream);
On the last line of my code, I am getting a NPE when I attempt to start my service. I assume this is because the file is not found.
I modified the manifest.mf as follows:
Class-Path: .
I also tried copying config.properties to the "lib" folder (subfolder to where my .jar file is located). Same results.
I modified the "set PR_CLASSPATH" line in the batch file that installs the service as follows:
set PR_CLASSPATH=MyService.jar;.
Still same NPE.
How can I get my code to recognize my config.properties file once the service has been installed?
Thanks,
Raymond
This is what I use to load resources in these situations and seems to work most of the time:
public static InputStream getResourceAsStream(String path) {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}
Could you check, if this helps in your case?
Another strategy to overcome this problem is the one described in my comment:
"copy the configuration file to a specific absolute folder path (i.e. c:\test) and change the classpath to point to that folder (set PR_CLASSPATH=MyService.jar;.;c:\test)"

How to acces txt file in netbeans build jar file?

So currently my netbeans project folders looks like this:
Block_Breaker <--Project
build
dist
Block_Breaker.jar
nbproject
src
packageONE
packageTWO
data.txt
manifest.mf
applet.policy
build.xml
I want to know how can i acces a data.txt file in packageTWO(when i run Block_Breaker through a jar file and not netbeans). Normally if run through netbeans the following code will work:
FileWriter x=new FileWriter("src/packageTWO/data.txt");
PrintWriter pr=new PrintWriter(x);
But if i run a jar file that netbeans created it doesnt work.
You can't write to that file once it is packaged into a jar file.
Yet reading is still possible using one of the following:
<YourClass>.class.getClassLoader().getResourceAsStream("packageTWO/data.txt");
// or
this.getClass().getResourceAsStream("/packageTWO/data.txt");
witch gives you an InputStream witch you can use to retrieve the content of the file.
If you are required to wite to that file then the simplest way is not to pack it into the jar but have it standalone some where on the filesystem.
More infos about getResourceAstream in the javadoc
This is because your .jar file does not include a folder named src/
Please use ClassLoader.getResource to load resources.

Null pointer exception while reading properties file

propsdatabase = new Properties();
InputStream dbin = getClass().getResourceAsStream("/properties/database.properties");
propsdatabase.load(dbin);
I am reading my database connection details via a properties file which is named 'database.properties' in a folder named 'properties'. Jus below the root directory.
The code was perfectly working fine when the jar was exported in Eclipse.
But I used Maven project in IntelliJ to get the jar . It throws NUll pointer exception .
Since the Value of dbin is NULL.(I printed and checked also).
I conclude that the path is not recognised to read the file.
Now The things are fine with IntelliJ .
While doing an export as jar in Eclipse the jar although contains propertioes folder IT is not detected. pl help
The reason that getResourceAsStream is returning null is that the /properties/database.properties file is not in the Maven classpath.
Move your properties folder to under /src/main/resources folder and when Maven creates a jar file, the /properties/database.properties resource will be included, and you'll stop getting the NPE.
Yes, getResourceAsStream is no doubt returning null. I suspect that your jar file doesn't include the file. Try running:
jar tvf yourfile.jar
to check. Next, check the build steps for the jar file. It's hard to say what's wrong without seeing the build file, so please edit your question to include that information if it doesn't leap out at you when you look at the steps.
Does your maven build step include the properties file in the jar? Check the jar-file that is produced. If you don't know how you can always rename it and add a ".zip" at the end and open it.
You may try to read the properties file using the path and a FileInputStream:
Properties properties = new Properties();
FileInputStream input = null;
try {
input = new FileInputStream(new File(CONFIGURATION_FILE));
properties.load(input);
}catch(...){...}

Java - Classpath issue

I have a products.jar file. Inside that there is one class com.ClassA. The structure of the project is like
products
|--test
|--com
|--ClassA.java
|--dependencies
|---inputs.txt
Inside eclipse, ClassA.java is accessing the inputs.txt file by the following path and it is working fine
private static final String PROPERTIES_FILE_PATH = "test/dependencies/inputs.txt";
test package is in the java build path -> sources
But when I am exporting this project as products.jar, I find that in the jar file there is no test directory. There are two dirs com and dependencies at the root of the jar file. So when I am trying to execute the ClassA (residing in jar file) through command line, I am getting the following exception:
JUnit version 4.8.2
java.io.FileNotFoundException: test/dependencies/inputs.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.io.FileInputStream.<init>(FileInputStream.java:96)
So after finding that test dir is not being exported in jar file, I changed the path of the file in my ClassA.java to dependencies/inputs.txt. It didn't work in eclipse but I thought that it would work in jar because jar file is in classpath and dependencies folder is at the root of the jar file so the java launcher will be able to locate the dependencies folder and then inputs.txt file.
But unfortunately it is also not working.
You can't use FileInputStream to read a file packed inside a JAR - it's not a file any more. FileInputStream only works for actual disk files.
You need to read the resource using Class.getResourceAsStream (javadoc), e.g.
InputStream stream = getClass().getResourceAsStream("/dependencies/inputs.txt");
You leave off the test prefix because it's not in the JAR file structure.
FileInputStream() will not read inside your jar, only files on the file system. Use .getClass().getResourceAsStream(resourcename) or .getClass().getClassLoader().getResourceAsStream(resourcename)
More info on the javadocs for class getResourceAsStream and classloader getResourceAsStream()

Access a file from a JAR in the same folder

I need to acces (create and read) a file from a JAR file (executable jar),
and that file should be created in the same directory as the JAR
I tried
this.getClass().getResource("myFile")
but since the jar has packages in it, it won't work..
I also tried write just
File f = new File("myFile");
f.createNewFile();
and that works if i execute the JAR from the terminal, but if i execute the JAR by double-clicking it, the file is created in my home directory -.-''
how do i access a file being SURE that that file is in the SAME directory as the JAR file?
(of course also getting the jar absolute path would do the trick since i can get the parent folder from it)
This will give you the full path to the Jar:
String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
EDIT: sorry, was in javascript mode when I wrote that :). As was so politely requested, in a static method you should be able to do this:
String path = Me.class.getProtectionDomain().getCodeSource().getLocation().getPath();
(where the class name is Me).

Categories