How to get directory from .jar file into File object? - java

I have a .jar file and at root of them located a folder. How I can read this folder into java File class?

You can't use File, since this file does not exist independently on the file system. Instead you need getResourceAsStream() to get the contents of the folder, like this:
InputStream in = getClass().getResourceAsStream("/FILENAME.TXT");
BufferedReader input = new BufferedReader(new InputStreamReader(in));

Related

How to place a txt file in a Java Project?

I have a text file that I am trying to read from using a BufferedReader however I'm not sure where I should place this text file in my Java Project and also what that directory would be called.
For example:
File file = new File(Directory that needs to be specified);
From what I understood you are not sure where is your root directory, put your file in you project home directory instead of /src . For example if your project is called MyProject, put your file in .../MyProject/myfile.txt and your relative path would look like this: File file = new File("myfile.txt");
If you want your file in separate folder put it for example in /MyProject/resources/ and then your path would look like this:
File file = new File("/resources/myfile.txt");

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");

How to read .sql files internally in Java jar?

I currently have a folder of schema files in SQL that get executed when the application is executed. This is what I use to read and execute these files:
private static String getSchemaFromFile(String filename) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new FileReader("src/schema/" + filename + ".sql"));
String queryString;
.....
}
The problem I think is to do with the path of the schema folder. I tried looking at getResourceAsStream but I can't seem to get it working.
It works fine when I run from eclipse but when I compile it into a JAR it says file not found. How do I ensure the path is correct?
Something along these lines. BTW this will probably fail unless you are running your jar (executing it in eclipse probably won't work).
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("src/schema/" + filename + ".sql")));
From eclipse it works because eclipse enable java code to search from src/schema/ directory.But when you are using jar it requires an absolute path.
I will suggest store the absolute path where you place your .sql file in a property file so that you can change whenever you need it.Suppose you from property file you have get the location absolutePath. Then you can do this -
BufferedReader bufferedReader = new BufferedReader(new FileReader(absolutePath + filename + ".sql"));
If the schema is in a jar file, then src/schema is probably not valid, more likely it will be in schema/. First you need to confirm this. The jar file is actually a zip file, so you can unzip it with utility like winzip to examine the contents.
Once confirmed where the schema files are located, they cannot be accessed using FileReader, as they are not in the file system but inside your jar. To access a file from within the jar file, getResourceAsStream is the correct approach. You probably defined the wrong folder when you tried it last time.

access my jar file items in eclipse

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>");

Where to place a file within a Java Web Project?

I have a small java program that reads a file in, in eclipse i have the file in the main project dir and the class file is within the src dir. This works fine.
I want to you this small piece of code within a web project im working on, currently i have the class file in src/tools/, but im lost on where to put the file?
I have tried it in a few places yet it throws file not found.
Where is the best place to store this file? and how can i ensure i have the right path when using the following code?
FileInputStream fstream = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
You either need to load the file as a resource (e.g., getResourceAsStream(), use a path relative to the app (getRealPath()), or put it in an absolute location and use a full path.
If you put the resource file in the root of your classpath (so if your source path for java files is src/main/java then put the resource file in there) then you can do this.
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("myresource.txt")));
Now if you want you can put it in a package, so the file would be in src/main/java/com/sksamuel
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("com/sksamuel/myresource.txt")));
Finally, if you're using maven or something similar, then you would put the file into src/main/resources and not src/main/java, and at compile time the two paths are combined, so you would use the same code as above.
My answer assumes the file will be packaged up with your java code and is something that would change by a developer. If it's generated, or needs to be changed outside the jar/war then this isn't the best way.

Categories

Resources