I would like to ask how to define path to files stored directly in my project folder. When i generate .jar file, xml file is stored in main folder in .jar.
private static final String PATH_TO_XML = "xmlOutput.xml";
This is working when I run project in Netbeans IDE. However, executing .jar file throws exception that file cannot be found. I tried some ways but i cannot find the way to specify path to file to be working in IDE and also as .jar file.
I read information about access as stream. However, my application executes XSLT transformation using XsltTemplate.xsl, xmlOutput.xml and htmlOutput.html so I need to use this file for transformation.
Thanks for help.
It might help to think about where the referenced files are stored in each context. If these are not packaged in the JAR like they are located in the IDE's project, then they won't be found. It might be best to configure the location in some configuration file and/or use an absolute path which is also available when running the JAR.
Related
I have a Spring app that I am deploying as a .jar.
The app has to write to a folder located in /src (precisely /src/main/resources/patches). I have this path directly in the code.
In application.properties: PATCH_DIR = src/main/resources/patches
The app has to also read a json file from src/main/resources/myJson.json, the path also being directly written in the code.
Prior to deploying, while running from the IDE everything goes well, the app sees the file and the folder and reads and writes correctly.
After building the .jar the paths change, the file is located in myJar.jar/BOOT-if/classes/myJson.json and the folder is respectively in myJar.jar/BOOT-if/classes/patches.
How can I specify these paths in the code in a way that even after building the jar they stay relevant to my application?
Edit: I can specify the path of the file as: PatchApplication.class.getClassLoader().getResource("myJson.json").getPath();
This should solve the problem, as the path is relative to the class and not to the root of the project, but it does not improve anything.
You should specify a path in your file system,instead of the path inside a .jar.When you run your app ,it would access the given path.
generally all the classes goes to classes folder in jar.
all the classes ending after src/main/java goes to /classes/ folder
and similarly all the resources file
Eg. your source folder have src/main/java/com/mypack/ABC.java than you will find this in jar as /classes/com/mypack/ABC.class .
Try using /classes/patches and /classes/myJson.json.
This should work
We have a jar which needs to read a properties file. The properties file needs to edited without rebuilding the application, therefore it is excluded from the build.
In order for the application to see the properties file, it is placed it a folder which is on the java classpath. The format of the Windows command line used to run the application is as follows:
java -cp application.jar;.\lib_folder\*; com.company.Start
Java correctly picks up all the jar files in the lib_folder. The properties file is placed in the same folder, but the application cannot see it and is throwing a FileNotFoundException. Are we doing anything obviously wrong?
Thanks very much
The class resolution happens starting from the path given in cp. If you provide .\lib_folder in the classpath without the star, and the properties file is inside this folder, then it will be picked up. Currently the properties file itself is in the classpath.
it is placed it a folder which is on the java classpath
Your class path only contains JARs, the notation .\lib_folder\* means every jar from a folder, but not the folder itself.
The properties file is placed in the same folder, but the application cannot see it
The folder itself need to be added, however I suspect you shouldn't be adding files to the "lib" directory and you should be using a different directory like "config"
and is throwing a FileNotFoundException.
When you try to obtain a Resource you get null not this exception. Try the following
InputStream is = getClass().getClassLoader().getResourceAsStream("resource.properties");
This returns null if the file is not found.
Are we doing anything obviously wrong?
FileInputStream looks for a direct file, it doesn't use the classpath.
If you're using ResourceBundle to read the property files, then just add the top level project containing the .properties files to the classpath.
I was wondering if there is a way to add .dic and .aff files to a java project jar file (using eclipse for example)?
I have in my code a dictionary:
URL dicDic = CipherTextAttack.class.getResource("en_US");
static Hunspell.Dictionary dict = Hunspell.getInstance().getDictionary(dicDic.toString());
I need the jar file to run everywhere without needing the en_US dictionary file..
Is that possible?
Yes, a jar file is basically a zip file with a .jar extension, so you can put any file in the archive. You can then access that file from your code as long as it is in the classpath. One easy way to do it (but not so clean for big codebases) is to put the file in the same directory structure as your class files.
To access the file, you can use Class.getResource() as you show, giving a path relative to the class used, and it will be searched using the class loader of the class used.
So in your use case, the easiest is probably to put the file in the same directory as the class using the dictionary. For example, in your code is in MyClass, you would write:
URL dicDic = MyClass.class.getResource("file.dic");
C.f. the javadoc of the method.
Then to add the files in the jar, this will depend on your workflow and how your build your project (using Eclipse, ant, maven, etc). For example, if you use ant to compile and package your project, there must be somewhere in your build file a jar task that creates the jar file. You should then modify that task to include the dic file in the jar file. In case of doubt, and if you can't find an existing answer, I'd suggest opening a separate question about your particular tool.
In any case, for the purpose of the test, you can simply open the jar file with Winzip or 7-zip or whatever zip file manager that you use, and add the dic file to the archive.
I have a maven project with typical project structure. At the base of the project, I have a directory names res that has some resources (> 500 MB).
I am trying to use
this.class().getClassLoader().getResourceAsStream("res/xxx")
this code fragment to read from that folder, but it returns a null resource stream.
I have a few workarounds below, but none of these are acceptable due to reasons explained below.
I can move the folder to {base}/target/classes, and it will be read, but it will also get cleaned when I do a mvn clean. Hence, this approach doesn't work. For some reason, specifying the path as ../../res/xxx also doesn't work.
I can move the folder to {base}/src/resources, but then it will get copied to target/classes and the jar. Hence this is also not acceptable.
Though I am open to trying some other java APIs, I may have to use the class loader mechanism only as there is some external library component that is also trying to access the res folder using the similar approach.
Is there some way I can read the res folder from projects base directory? Is there some setting in pom.xml file that can help me with that?
Use this.class().getClassLoader().getResourceAsStream("/res/xxx") and then you will be reading from the root of the classpath irrespective of the actual file/folder location in windows or Linux. This is actually one of the safest ways to read files especially when you do not know how your application will eventually be deployed.
It means though that your classpath must include the parent of res and that res/ must be copied over when you deploy your app
Otherwise, if you want to use relative paths, try this snippet (taken from this SO answer):
String filePath = new File("").getAbsolutePath();
filePath.concat("path to the property file");
If you use this.class().getClassLoader().getResourceAsStream("/res/xxx"), it is going to try to load resources from the classpath. If that's not want you want to do, you're going to need to specify an absolute path.
Resource on Classpath
If you don't want the resource built into your JAR, I'd suggest having a different maven project with the file in it, under the src/main/resources directory. This will create a jar file with the file in it, which will be ~500MB.
After this, you can include a dependency on this project in your project containing your application code. This will then be able to reference it from the jar file using getResourceAsStream(...).
If you don't want this large jar file to ship with your application, make sure you mark the dependency with <scope>provided</scope>.
File from Absolute Path
You will need to take the file location as a parameter in your main method, and then use new File("C:\\path\\to\\your\\file.txt") and then use a FileReader to read it in.
I was wondering if is possible to find the content in an XML file placed in a jar thath is placed in a ear too. It would help me find the properties of java beans.
Up into the ear I can iterate through documents and see what's inside, but if it is a jar I can't iterate documents inside that.
Someone can give me some advice?
From the ear file you should be able to extract the jar file. Then you can use WinZip, 7 Zip, etc to do explore the jar file contents the GUI. Or you can run the jar tf command to extract the content of the jar file in command line. If you don't have any of these tools and using windows, then you can rename the jar file to a .zip and windows should be able to explore it (most of the cases it works).
Edits - I am not sure if you wanted to do it using Java. In that case you are looking for JarFile. I found an example of it here for exploring Jar contents programatically.
so i just tested the thing you want to do - and as long as the JAR lies in the classpath of your EAR, then you can access any file within it. basically the try to look up the file from the context-root of your application.
for example if in your JAR the file abc.xml resides under the package a.b.resources, then from say a servlet in your EAR you can access it using :
InputStream is = this.getClass().getClassLoader().getResourceAsStream("a/b/resources/abc.properties");
Yes, you can read any file that is packed into zip file. It does not matter how many nested zip file you have to open on your way. Use ZipInputStream, get needed ZipEntry, read it content. If it is still zip, open it and do it again and again until you access the required resource.