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.
Related
I am trying to load a file from the resources folder, but I can't get it to work after build/packaging
URL databaseURL = this.getClass().getClassLoader().getResource("blacklisted.words");
List<String> blacklistedWordsDatabase = Files.readAllLines(Paths.get(databaseURL.getPath()), StandardCharsets.UTF_8);
This works perfectly when I run my code from the IDE, but after mvn package I run java -jar target/project-0.0.1-jar-with-dependencies.jar and get
java.nio.file.NoSuchFileException file:/var/www/project/target/project-0.0.1-jar-with-dependencies.jar!/blacklisted.words
But checking the archive blacklisted.words is clearly in the root folder of the jar... Any tips on what I might be doing wrong here?
You're using Files.readAllLines which expects a real file / path. This will work in an "unpacked" environment like when you're testing in IDE or running mvn test / mvn exec, but won't work with JAR, where files are packed inside an archive. There are no files and paths!
What you can do instead is to get an InputStream of a packed resource and use it:
try (InputStream resource = this.getClass().getClassLoader().getResource("blacklisted.words")) {
List<String> blacklistedWordsDatabase = new BufferedReader(
new InputStreamReader(
resource,
StandardCharsets.UTF_8
)
).lines()
.collect(Collectors.toList());
}
I have download.sh file in my src/main/resource folder in maven project and I am reading it through below code
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("download.sh").getFile());
The file is reading when I run this as the standalone application.
If I run this as application using jar ex:-
java -jar runScript.jar SCHEMA_NAME
/Users/IdeaProjects/RunScript/target/file:/Users/IdeaProjects/RunScript/target/RunScripts.jar!/download.sh": error=2, No such file or directory
Can anyone help me in reading file from resource when executing with jar
I think this error happens when you try to read the file. This is because resources inside your jar are not files but streams. So you should use
getClass().getResourceAsStream("download.sh");
and then read that stream, for example:
InputStream resourceStream = getClass().getResourceAsStream("download.sh")
BufferedReader br = new BufferedReader(new InputStreamReader(resourceStream));
String line;
while ((line = br.readLine()) != null) {
// do something
System.out.println(line);
}
I think you are not packaging your resources along with your jar. Please verify your jar contains the resource file (download.sh) with
jar -tvf <yourfile.jar> | grep -i download
Resources are not files. You can get their URLs, or input streams, but nothing you can do will turn a resource in a JAR file into a file on the disk that you can use with File. Short of unpacking the JAR file, that is.
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);
I'm working on a Java program in Netbeans that I'd like to be able to
run from an external jar file
And I cannot seem to read from a file that isn't located inside of
the project default directory or some subfolder located there, and
that only works inside Net beans.
What I'd like to be able to do is read the text file from the file
path
src/assets/files/textFile.txt
.
I've tried all of the suggestions here, but they don't seem to work for me. Here's the code I'm currently using:
File file = new File("assets/files/textFile.txt");
if (!file.exists()) {
throw new FileNotFoundException("File does not exist");
}
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
When I try to run this, the exception is thrown each time.
The term for such non-Files is resource.
String encoding = "UTF-8";
new BufferedReader(
new InputStreamReader(YourClass.getResourceAsStream("/assets/files/textFile.txt"),
encoding));
Also FileReader is an old utility class that uses the default system encoding. That might be different from where the text file was created, hence Unicode, such as encoded in UTF-8, would be ideal. For Windows users you might write a BOM character at the beginning to mark the file as UTF-8: "\ufeff".
The class YourClass should be from the jar. Here an absolute path is used "/...". A relative path would start with the package path.
Also Windows is not case-sensitive. Other systems and the zip format of the jar are case-sensitive. Check the jar with a zip utility for correct paths.
I have spent the past 3 nights going crazy trying to find an answer to this.
So I have a java program and I want it to be in a jar format and I want it to be able to read in text and image files.
I got the image files working fine using the this.getClass.getResource("") method, however I can not get the program to properly access the text files within the .jar, When I extract the jar, the text files are there so I know It is not a simple mistake of the text files not being within the jar
This is what I tried using, but it didn't work(It works without a jar, but now within a jar)
URL lurl = this.getClass().getResource("list.txt");
BufferedReader in3 = new BufferedReader(new FileReader(lurl.getFile()));
Fixes?
Assuming your class is
my.package.MyClass
The method will read the file from directory /my/package from JAR.
You can open the resource via:
BufferedReader in3 = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("list.txt")));