Put and Use .properties file outside Jar - java

I'm developing a Java Application in Netbeans. The application uses a .properties file (Properties class). The problem is that Netbeans insert this properties file into final .jar file.
How can I make Netbeans not to do this and put the properties file out of jar file?
In this case, how can I use the properties file in code?

Look at the source directories that NetBeans is asked to insert into the JAR and remove the .properties from the list.
You can always read the .properties file by reading it as an InputStream, as long as it's in the CLASSPATH.
The question is: Why do you feel the need to move it out of that JAR? At least it's in the CLASSPATH that way. You seem to be acting against your own best interests here.

when you export project by jar formart from Eclipse IDE, you can config it do not import file .properties into jar file.
If you want use .properties file in the same folder with file jar, you can get current path with code:
String path = <NameClass>.class.getProtectionDomain().getCodeSource().getLocation().getPath();
and now, you can load file .properties from above path.
Good luck to you!

Related

Defining paths to .jar file

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.

create a java-properties file beneath the .war file

When I export my Dynamic Web Project, I want that the .properties file is in the same folder as the .war file.
Oh and I need both the Windows and the linux variants of the solution if there is a difference. (I already read here how to create and read from a .properties)
I want the user to be able to edit the .properties file
I recommend setting your project up using mavens war plugin. You can add an option to put resources were you want in the war file. See the link below.
https://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html

How to add a .dic and .aff file to a java jar?

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.

how to place properties file in a directory in the classpath ahead of .jar in eclipse?

I want to place ABC.properties file in a directory in the classpath ahead of cmbview81.jar .
cmbview81.jar already has ABC.properties file in it. I extracted the file made some changes and made new ABC.properties file and now want to set its path ahead of cmbview81.jar file so that application will not point to the file which is in cmbview81.jar file.It should use new ABC.properties file.
I tried solutions provided here How to place a file on classpath in eclipse? but didn't work.
Put it in a separate Jar file and see to it that this Jar file is included in front of the existing Jar file in the classpath specification. The order of specification of the Jar files in the classpath is significant (it's the same idea as directly using the CLASSPATH variable).
You could also put it in your src somewhere, but just make sure then that your src folder occurs before the Jar files in the Order and Export tab of the Eclipse Java build path configuration.

Is it possible in Java read files placed in a Jar that it's placed in a Ear too?

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.

Categories

Resources