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

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.

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.

Can an external jar access a file within a folder in a project

I am using an external jar (stored in my lib file within an eclipse project) and that jar needs access to a file to which I am supposed to pass the path. So far I have only been able to make it work properly if I store the file in a completely separate area on the server.
I'd like to be able to store this file neatly within the project. For examples sake lets say that testfile.txt is in the projects src/testfolder. From within java I try to reference the file like so:
File file = new File("src/testfolder/testfile.txt").getAbsolutePath();
But that returns a path on my pc. In this case its:
"/home/me/testfolder/testfile.txt"
I'd like to application to be portable so I can move the jar file around if necessary without having to worry about bringing external folders. How can I reference this file within the application and pass that url to an external jar?
Does the jar includes this file as well. If yes, then it should not be an issue as the absolute path will be taken care of automatically.

Accessing files packaged into a jar file

I created a jar file with a runnable compiled class and some template files (text). I now want to use the text files packaged into the jar for distribution in the application as File objects.
I tried to just use relative paths which did not work. How do I get a File object pointing to a text file which is in the jar?
Is there a way to do this so it will work both when running the unpackaged class files and from a jar with the same code, or will I need to use separate functions?
from inside jar you can use it like Class.getResourceAsStream(String), or something similar.
from out side its not the file its jar so you will have to extract it in order to actually use inside file
If you are using the JAR within your application, then Class.getResourceAsStream(String) will do.
Alternatively, if you're using Servlet, then ServletContext.getResourceAsStream(String) will also do.
These 2 methods returns an InputStream (of your resource data) and not your File object as you rightfully want.

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.

Access .properties file at same level as working directory

I am working on a Java project that I want to deliver to my client as a .jar file. However, I want to allow the client to be able to change the parameters of the program without having to recompile or recreate the .jar. Basically, I want to be able to load .properties files from classes inside the .jar but locate those .properties files outside of the .jar and even outside the working directory.
I have been testing my attempts inside Eclipse, which might be causing some of the problem but I don't see how at the moment. My setup is a follows. I have one project that contains a few classes that I build a .jar file from. I have a .properties file that is used to create a ResourceBundle whenever a class for the .jar is created. I specify that an additional directory, "conf/", be included in the .jar classpath within the .jar manifest.
Once the .jar file is built, it is copied to the lib/ directory of another project which I am using for testing. This test project includes the .jar file as a library ("Add External Jars..") in the Java Build Path. The .properties file is located the conf/ directory which is at the same level as lib/, src/, and bin/ but I am unable to accces it there. The only way I have been able to get it to work is to locate conf/ under src/ (and bin/) but I would like to be able to use it up one level. Is this possible?
Here's the entry in the .jar manifest file...
Class-Path: ../conf/
Here's the ResourceBundle call that I tried (didn't work)...
rb = ResourceBundle.getBundle("..conf.BaseProject");
Here's the directory structure that works now (names have been changed to protect the innocent)...
/Project
/Project/bin
/Project/bin/conf
/Project/bin/conf/BaseProject.properties
/Project/bin/TestClass.class
/Project/lib
/Project/lib/BaseProject.jar
Here's the directory layout I want (again, file names not important)...
/Project
/Project/bin
/Project/bin/TestClass.class
/Project/conf
/Project/conf/BaseProject.properties
/Project/lib
/Project/lib/BaseProject.jar
You can get it using the Classloader.
I like Spring's ClassPathResource

Categories

Resources