I was having some issues getting the executable jar file for my project to work (outside of the eclipse IDE). I figured out the issue with java -jar fileName.jar
Here's the code:
fileIn = new Scanner(new File("src//resources//TestSave.txt"));
Through a little troubleshooting and research I understand that this wasn't working in the executable jar because it doesn't have a src folder, that's only created within the IDE. Here was my solution:
fileIn = new Scanner(new File("D://resources//TestSave.txt"));
The only problem with this is that if I want to put this program on my resume then who ever views it will have to place the folder into their D drive. I want it to be quick and easy so that they can just view my project with no hassle.
How can I access the resources folder within the executable jar file itself without having to reference/create any outside folders?
You have two options.
put the file in the same file as executable jar fileIn = new Scanner(new File("./TestSave.txt"));
Follow this post reading-a-resource-file-from-within-jar
Use getResourceAsStream (no File) within a JAR:
InputStream inputStream = classLoader.getResourceAsStream("TestSave.txt");
Scanner input = new Scanner(inputStream);
Related
I'm new to NetBeans IDE, and am struggling with accessing a file after building the jar file. After reading through many posts on this topic, I decided to try the following code:
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/file.txt")));
This works fine when my file is placed inside the "build" folder of the project where the .class files are, but of course this is a problem because it is erased in the "clean and build" process when the jar file is created. I have tried placing it in the src folder, in a separate "resources" package, and in the root of directory. I have also tried calling getResourceAsStream() with "file.txt" and "/src/file.txt," but it only works in the above configuration when the file is with the .class files. Any tips would be much appreciated!
Why not have your file folder inside the tomcat bin and refer the directory from your code. So maven clean will not alter the files and you can remove, update file without needing to restart the application. ( here i have file inside etc )
Path: /Users/username/Documents/apache-tomcat-8.5.15/bin/etc
ArrayList<String> readList = null;
String workingDir = System.getProperty("user.dir");
String fileName = "File.txt";
File file = new File(workingDir+"/etc/" + fileName);
readList = resourceReader.readFile(file.getAbsolutePath());
I have method readFile to parse some data and build the ArrayList in the above example.
Read about System Properties
Turns out the solution was really simple...I had been trying to manually create a resources folder, but the contents kept being deleted upon building of the jar. Instead, I created a resources package and put the file into the auto-generated folder inside the src folder, which packaged the file into the jar. Thanks everyone!
FileInputStream fstream = new FileInputStream("abc.txt")
is throwing a FileNotFoundExceptionn while running as a jar. Why ? Normally it is able to find while running from main method.
class MyClass{
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
}
This code should be used.
And the files(in this case abc.txt) should be kept , in the Object references class location. That means , this.getClass refers to the location of some folder i.e, com/myfolder/MyClass.java folder .
So we should keep the abc.txt in com/myfolder this location.
If your file is packaged with your jar then you should to get information using getClass().getResource(url):
FileInputStream inputStream =
new FileInputStream(new File(getClass().getResource(/path/to/your/file/abc.txt).toURI()));
Else you need to create it always in the same path with your jar and you can get it like you do :
src/myJar.jar
src/folder/abc.txt
FileInputStream fstream = new FileInputStream("folder/abc.txt");
You can read here also :
How do I load a file from resource folder? and File loading by getClass().getResource()
You can use FileInputStream only when you actually have a file on the computer's filesystem. When you package your text file in the jar file for your program, it is not a file in the filesystem. It is an entry inside the jar file.
The good news is that it is even easier, in Java, to access the file this way: it is in your classpath, so you can use getResourceAsStream().
InputStream stream = getClass().getResourceAsStream("abc.txt");
If you have your classpath set up correctly, this will work regardless of whether it is a file in a directory (such as during development), or an entry in a jar file (such as when released).
It's because your working directory will probably be different under the two environments. Try adding the line
System.out.println(new File("abc.txt").getAbsolutePath());
to see where it is actually looking for the file.
I'm working with Eclipse and there, I have Java Project with the name "Test" which also contains text files. A class in this Project should be able to read in one of these files with a BufferedReader. This is my current code for that:
BufferedReader in = new BufferedReader(new FileReader("C:/Users/workspace/Test/testFile.txt"));
My file is always in the Project, but when I move the Project to another path, the file path changes, too, so I have to adjust the code with the new path.
I dont't want that, because it's impractical, so what can I do? How can I get the path of the Project?
You can add file to resources folder and read like
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
Try something like this:
File currentDirFile = new File(".");
String fileDir = currentDirFile.getAbsolutePath();
This is the code I am running in Java project to read from a text file:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("wordsEn.txt").getFile());
BufferedReader br = new BufferedReader(new FileReader(file);
When I run this program in Intellij IDEA, everything works fine, but when i build its JAR file, put it in my desktop, and run it through command line from my desktop with the command java -jar SyzygiesGudrat.jar it gives FileNotFoundException
Where do I have to put the text file in order to run this JAR from anywhere or in order someone else to be able to run this JAR when I send it to him?
My project structure looks like this:
Changed the code to the below one, as #MadProgrammer suggested. Now everything works fine.
InputStream in = this.getClass().getResourceAsStream("wordsEn.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in);
I stucked with the same issue.
Solution is : you can create a jar by IDE or command prompt.
while running a jar in your folder where you store a jar must give txt file.
For example, if you save jar on desktop, then on your desktop must have txt file before you run a jar from command prompt.
I'm programming Java in Eclipse IDE. Here is code I want to read file:
File file = new File("file.txt");
reader = new BufferedReader(new FileReader(file));
I put file.txt in two place:
1) same folder of this SOURCE file.
2) in bin\...\ (same folder of this CLASS file)
But I allways receive NO FILE FOUND.
Please help me.
thanks :)
If the file ships with your application, it would be better accessed as a resource than as a file. Simply copy it to somewhere in your build path and use Class.getResourceAsStream or ClassLoader.getResourceAsStream. That way you'll also be able to access it if you bundle your app as a jar file.
Currently, you're looking for the file relative to the process's current working directory, which could be entirely unrelated to where the class files are.
if you put the file under sources and inside the package "test" for example, the path is:
./src/test/file.txt
you can use
File file = new File("./src/test/file.txt");
System.out.println(file.exists());
The path ./bin/test/file.txt will work in the second case and is more suitable for a normal java project