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
Related
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
I have a folder called lib that contains all my Jar files and in one of the Jar files class, I have a main method which is called by a batch file. In the same folder location as my lib, I have another folder structure path/to/a/resource/myresource.txt
How can I load this file from a class inside the Jar file? I tried the following and both resulted in null:
getClass().getResource("path/to/a/resource/myresource.txt")
getClass().getClassLoader().getResource("path/to/a/resource/myresource.txt")
Any ideas? Even with an absolute path, it failed! Any suggestions?
You can use:
getClass().getResourceAsStream("path/to/a/resource/myresource.txt")
However, for this to work, you need to add the path '.' to the Class-Path entry of the JAR's MANIFEST.MF file.
http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Two things you tried are used to read files from class-path since this folder is not on your classpath you can read it directly with any of the java File IO classes.
File file = new File("C:/folder/myFile.txt");
or if you know the relative path:
File file = new File("../../path/myFile.txt");
Your path seems not to be precise enough. Further, this question has been worked before.
Have a look here:
How to Load File Outside of, but Relative to, the JAR?
How to get the path of a running JAR file?
You can either load the file from file system
new FileReader(relativeOrAbsoluteFilesystemLocation)
or you can add the directory in question to your classpath:
java -cp "lib/*;lib" ...
and then use your original method.
(Unix uses : rather than ; as classpath separator)
I have a java project for which I have used Intellij Idea. The project runs fine, and I have exported it as a jar. The jar export also runs properly, except for one issue:
I have an additional "resources" folder within my project, and there are two csv files (very simple structure with two columns). In the code, I have used their relative paths as follows
private static final __MY_FILE = new File("resources/filename.csv");
As expected, this works properly when I run from the IDE. But when I export the project as a jar, the code crashes with FileNotFoundException because these relative paths are no longer treated as being relative to project folder.
Temporarily, I have resorted to providing the full path in the code (i.e. __MY_FILE = new File("/home/.../resources/filename.csv")), but this is clearly a disastrous practice!
How can I use paths relative to the project folder so that the exported .jar works?
The directory structure is as follows:
networkmeasurements
/matrixmethods
/src
/probabilisticmethods
/src
/utils
/src
/resources
filename.csv
I am trying to get the resource filename.csv from a class inside matrixmethods.
You can't use File, since this file does not exist independently on the file system. Instead you need getResourceAsStream(), for example
...
InputStream in = getClass().getResourceAsStream("/resources/filename.csv");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
...
http://www.jetbrains.com/idea/webhelp/resource-files.html might have good insight. You have to get Intellij to recognise .csv files as property files so it will bundle them.
I have a problem where I can't seem to link to a xml file, see the layout below:
Folder Name
-Folder
-Folder
-SourceFiles
-packagename
-all my java files
-myXml.xml
Build is where all the class files etc is stored.
src is where the projectFolder is, and within it the java files
Code I am using to link XML File for Synth: SynthDialog.class.getResourceAsStream("synthtest/synthDemo.xml")
Now I want to link to the myXML.xml file in the top-level folder. It would be the PHP Equivelent of ../../Folder/
Thanks
You appear to be attempting to access the file using getResourceAsStream with a relative name. If that is the case, then the resource should be in located in a JAR file or directory on the classpath, and the location will be resolved relative to the FQN of the class.
I can't tell where the ".class" files are located in the tree, or how your classpath is set up, so I can't be more specific.
UPDATED
If you are executing out of that build directory, then your build process needs to copy the XML file to the appropriate place in the build tree so that the class-relative path ends up referring to the file. (Or use a path that starts with "/" so that you don't depend on the classes FQN at all.)
In the long term, you will probably execute out of a JAR file, and the data file will need to be inside it.
Use "getSystemResourceAsStream" instead of "getResourceAsStream" to access files outside of your codebase.
Suppose I have a Java class that needs to access a file with absolute path
/home/gem/projects/bar/resources/test.csv:
package com.example
class Foo {
String filePath = ????? // path to test.csv
String lines = FileInputStream(new File(filePath).readAllLines();
}
Where the path to Foo.java is /home/gem/projects/bar/src/com/example.
Of course I cannot specify absolute path to the resource file. This is because jar file will be distributed as library for any clients to use in their own environments.
Assume the resource file like test.csv is always in the same path relative to project root. When a jar is created containing Foo.class, this jar also contains test.csv in the same relative path ( relative to project root).
What is the way to specify relative path that would work no matter where the project bar is moved to? Also how can I create a jar file (which can be in any location) so that the path to the resource file test.csv would still be correct.
To keep things simple, I have used invalid Java API ( readAllLines() which reads all the lines and return a string containing entire file content. Also not using try/catch).
Assume csv file can be read as well as written to.
I hope this makes it clear now.
Put the test.csv file into the src folder and use this:
Foo.class.getResourceAsStream("/test.csv")
To get an InputStream for the file. This will work wherever the project is moved, including packaged as a JAR file.
Example:
ProjectX\src\Test.java
ProjectX\resources\config.properties
If you have the above structure and you want to use your config.properties file, this is how you do it:
InputStream input = new FileInputStream("./resources/config.projects");
In this example you don't have to worry about packaging your source into jar file. You can still modify your resources folder anytime.
Use getResource(), as shown here.