access my jar file items in eclipse - java

I have two jar files "jar1.jar" and "jar2.jar" in my classpath
now there is a file test.xml in "jar1.jar" , which can be read from my program
by this
x=reader.readXml("test.xml");
but if i try to access some file from "jar2.jar" my program is not reading it,
is there a way to tell program that you need to read from this jar file, because its only reading from jar1 but not jar2..
thanks

make jar1 in your class path,
In any class in jar2, try this:
InputStream is=this.getClass().getResourceAsStream("/path to xml/test.xml");
BufferedReader br=new BufferedReader(new InputStreamReader(is));

You will have to adjust import order for your jar files. Go to project->properties->Java Build Path->Order and Export and make the jar2 up in the export order. I presume You have same file in both jar files.
If your file in jar2 has different name so, you have to verify:
This jar file is in your class path
Your pointing to a correct XML file location.
After above checks, you can use following line of code to read file.
InputStream stream = getClass().getResourceAsStream("<valid path to xml>");

Related

Read a file from a jar file, the jar generated using netbeans

I have a java project which should read file in a external folder. I give the file path as '../FolderName' in the class. But if I generated a jar file from this project, it cannot read the file. What is the correct way to define the folder path in this kind of situations ?
You should use java.lang.Class.getResourceAsStream(String).
It reads the file from inside your JAR.
Example:
InputStream in = getClass().getResourceAsStream("/classpath/to/my/file");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
A good Reference : How to read a file from a jar file?
The correct way to pass the absolute path to your code (jar) as program argument.
java -jar myjar.jar abs_path
You can then access the absolute path in the main() method of your class (mentioned in the manifest.mf file of a JAR) as follows:
String filePath = args[0];
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/classpath/toyourfile");

File doesn't read after creating jar using netbeans

i have a small application which checks for values from a file and display the result in a jframe.
A file contain list of word to check. this file is placed in project folder "testing" and the main source testing.java file is present in location "testing\src\testing"
input file : c:\document..\netbeans\testing\
java file : c:\document..\netbeans\testing\src\testing\
when i place the input file inside folder "c:\document..\netbeans\testing\src\testing\
" the input file is not taken as input, it works only when kept on folder "c:\document..\netbeans\testing\"
so when a jar file is created it has not included the input file in that, even i manually input that is not taking the input file in and working.
some path setting issue? what can be done to solve this issue?
any help pls??
Once you create the jar, the file becomes an embedded resource. If you try to read it as a File it will no long be the same file system path as you originally use in the program. It must now be read from the class path.
To read the file from the class path, you will want to use getClass().getResourceAsStream(), which return an InputStream. If your file is in the same location (package) as your class file, then you should use
InputStream is = getClass().getResourceAsStream("input.txt");
Then you can read from the InputStream
BufferedReader reader = new BufferedReader (new InputStreamReader(is));
This generally happens, when you don't use absolute path...!
As when you run your program from IDE(Netbeans) then the HOME_FOLDER is your ProjectFolder. Relative to which you would have given the file_path(that has to be accessed in your program).
But after building, jar is present in ProjectFolder/dist. When you run the jar file the HomeFolder is not ProjectFolder rather it is ProjectFolder/dist.
So, to make it successful, to need to copy all files and folders from ProjectFolder/dist to ProjectFolder.
Then run the jar.. Hope it will fix the issue
Try putting double backslashes in your file paths. Like this:
c:\\document..\\netbeans\\testing\\src\\testing\\
This is the format that java normally requires it to be in

Modifying BufferedWriter argument in order to make JAR file

I am using BufferedReader and BufferedWriter in a progect in order to export it as Jar and use it,
My code using the streams is the following
br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/data/score.scr")));
bw= new BufferedWriter(new FileWriter("src/data/score.scr"));
I tried to use getClass().getResourceAsStream(); inside BufferedWriter but it doest work, the program works well but it seems ... that the file read is not the one written ... I save a score (BufferedWriter) when I get to the scorepanel (BufferedReader) it is not there
all this in order to export my project as a JAR so I need to modify the second line of my core
In general you cannot write inside a jar file but here is a more detailed answer How can my Java program store files inside of its .jar file?
You could try using your IDE to do this.
eclipse
or through command line
jar cf jar-file input-file(s)
From what it looks like your doing, you don't necessarily need to store data in the jar itself. Just write the data to the same directory or folder as your jar and have your user transfer the entire folder as part of your program.

Java: How to package and access resources inside a runnable Jar file?

I have a Java console applicaton with a resources folder. It works OK, but when I want to export my code to a runnable Jar file(with Eclipse), then the exported Jar can not find the files(they are in buildpath) and give a Filenotfound exception.
When I unzip the exported Jar file I can see the files are there in the root folder, so I guess something wrong with my code.
BufferedReader srcFile = new BufferedReader(new FileReader("resources/"+filename));
String line = srcFile.readLine();
I tried to use
URL url= ClassLoader.getSystemResource(filename);
filename=url.tostring();
But no luck.
Use getResourceAsStream(String) (docs) if you want to read in a resource on the classpath.

Where does Java put resource files when I JAR my program?

Been looking for this for the past 2 hours and can't find anything (I've found solutions to the same problem but with images, not text files).
Pretty much, I made a program that reads a text file. The file is a list of names and IDs. Using Eclipse, I put the file in my src folder and in the program put the path file to it. Like this:
in = new BufferedReader(new FileReader(curDir+"\\bin\\items.txt"));
Where curDir is the user's current directory (found with System.getProperty("user.dir")).
Now, problem is, the program runs fine when I run it from Eclipse, but when I try to make it a runnable JAR and then run it, the program runs, but the info from the text file does not load. It look like Eclipse is not putting the text file with the JAR.
EDIT: Solved-ish the problem? So the JAR file needs to the in a folder with all the original files? I am so confused, what is a JAR file then?
A more robust way to get a file whether you are running from Eclipse or a JAR is to do
MyClass.getResource("items.txt")
where MyClass is a class in the same package (folder) as the resource you need.
If Eclipse is not putting the file in your JAR you can go to
Run -> Run Configurations -> -> Classpath tab -> Advanced -> Add Folders
Then add the folder containing your file to the classpath. Alternatively, export the Ant script and create a custom build script.
To the point, the FileReader can only read disk file system resources. But a JAR contains classpath resources only. You need to read it as a classpath resource. You need the ClassLoader for this.
Assuming that Foo is your class in the JAR which needs to read the resource and items.txt is put in the classpath root of the JAR, then you should read it as follows (note: leading slash needed!):
InputStream input = Foo.class.getResourceAsStream("/items.txt");
reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
// ...
Or if you want to be independent from the class or runtime context, then use the context class loader which operates relative to the classpath root (note: no leading slash needed!):
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("items.txt");
reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
// ...
(UTF-8 is of course the charset the file is encoded with, else you may see Mojibake)
Get the location of your jar file
Firstly create a folder(say myfolder) and put your files inside it
Consider the following function
public String path(String filename)
{
URL url1 = getClass().getResource("");
String ur=url1.toString();
ur=ur.substring(9);
String truepath[]=ur.split("myjar.jar!");
truepath[0]=truepath[0]+"myfolder/";
truepath[0]=truepath[0].replaceAll("%20"," ");
return truepath[0]+filename;
}//This method will work on Windows and Linux as well.
//You can alternatively use the following line to get the path of your jar file
//classname.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Suppose your jar file is in D:\Test\dist
Then path() will return /D:/Test/dist/myfolder/filename
Now you can place 'myfolder' inside the folder where your jar file is residing
OR
If you want to access some read-only file inside your jar you should copy it to one
of your packages and can access it as
yourClassname.getResource("/packagename/filename.txt");

Categories

Resources