This question already has answers here:
FileNotFoundException in src/main/resources
(4 answers)
Closed 2 years ago.
I am trying to build my project to a jar file. When I first opened the jar file I noticed nothing happened. I then launched the jar file from cmd. Then I saw it throws an error because it cannot find a txt file. This is the error:
Executing the program in IntelliJ works fine so I am assuming the txt file is not included in the artifact. This is how I use the txt file in the code:
This is what my file structure looks like:
This is what my artifact settings look like:
I tried putting the txt file in the src folder and manually including it in the artifact settings, I tried putting the txt file in the resources folder (It is marked as Resources Root) and I tried using the resources folder and also manually adding the resources folder to the artifact settings (this is the last thing I tried and is visible in the picture of my artifact settings).
I am really confused why the txt file doesn't seem to be in the jar file. Any help would be really appreciated.
Place the file in the resources folder, and instead of File try using getResourceAsStream to get the contents of the file:
InputStream in = getClass().getResourceAsStream("/data.txt");
Scanner pathScanner = new Scanner(in);
Make sure to set ?*.txt as resource pattern in the compiler options.
Related
I have tried many variants but I cant find correct.
I have something like
Inside my jar, which created by Maven I can see that
That is my folder with classes. And, by the way, If I start my program in IDEA, not from Console, there is not any exception with paths
Here, I am in debug mode start my jar trying to see, where is the problem.
If I do 'file.exists()' it would be false but file inside. I think, that problem because of '.jar!\' in the path, but I don`t know how to remove that.
Anyway I've tried absolute and relative path, I've tried
Thread.getCurrentThread.getContextLoader.getResource()
GUI.class.getResource()
GUI.class.getClassLoader.getResource()
Nothing help
You can't use File to open resources inside a jar file. File can only be used with normal files and directories.
Note: using File works fine within in the IDE, since all files are not packaged in a jar file yet. But the program will break after you package it.
Once you locate the resource eg. URL res = GUI.class.getResource("/rxtx64/myres.dll") , you can open that resource as a stream InputStream is = res.openStream(); .
See also related answers Utils to read resource text file to String (Java) and How to read a text-file resource into Java unit test?
This question already has answers here:
How do I read the manifest file for a webapp running in apache tomcat?
(7 answers)
Closed 8 years ago.
I want to use my manifest.mf file as an InputStream so I can retrieve some of the data that is inside. I use the following line of code:
inputStream = new FileInputStream("../../../../WebContent/META-INF/MANIFEST.MF");
Because the java class and the manifestfile are located in the following directory:
Unfortunately, this path always give me a FilenotFoundException. What is the correct path to refererence this file?
You want to load a file that is bundled with your web application application. This file will be part of the war file of your deployed app. It thus won't be on the file system. So loading it with a FileInputStream is not the right solution.
BTW, file paths are not relative to the class creating the FileInputStream. They're relative to the directory from which the application server is launched.
The way to load webapp resources is to use the ServletContext.getResourceAsStream() method. Read its javadoc (and the javadoc of ServletContext.getResource()) carefully.
You should also realize that WebContent is the name of the directory where the sources of your webapp are. Once packaged and deployed on a server, there won't be any WebContent directory anymore.
try this:
File file = new File("../../../../WebContent/META-INF/MANIFEST.MF");
System.out.println(file.getCanonicalPath());
see where your app think the file is located and fix the path
So currently my netbeans project folders looks like this:
Block_Breaker <--Project
build
dist
Block_Breaker.jar
nbproject
src
packageONE
packageTWO
data.txt
manifest.mf
applet.policy
build.xml
I want to know how can i acces a data.txt file in packageTWO(when i run Block_Breaker through a jar file and not netbeans). Normally if run through netbeans the following code will work:
FileWriter x=new FileWriter("src/packageTWO/data.txt");
PrintWriter pr=new PrintWriter(x);
But if i run a jar file that netbeans created it doesnt work.
You can't write to that file once it is packaged into a jar file.
Yet reading is still possible using one of the following:
<YourClass>.class.getClassLoader().getResourceAsStream("packageTWO/data.txt");
// or
this.getClass().getResourceAsStream("/packageTWO/data.txt");
witch gives you an InputStream witch you can use to retrieve the content of the file.
If you are required to wite to that file then the simplest way is not to pack it into the jar but have it standalone some where on the filesystem.
More infos about getResourceAstream in the javadoc
This is because your .jar file does not include a folder named src/
Please use ClassLoader.getResource to load resources.
This question already has answers here:
How to get the path of a running JAR file?
(33 answers)
Closed 9 years ago.
How to reference directories correctly when launching a .jar from a batch file?
I am trying to launch a jar file from a .bat . One of the first things my code does is open and read from a file. I reference the file like this:
final ArrayList<EmailAccount> emailList = FileIO.getListOfAccountsFromFile(".\\EmailList\\list.txt");
Everything works fine if the batch file is in the same directory as my jar. HOWEVER, if I put my batch file somewhere else and try to run it (like i plan to have it work), the program thinks that I'm trying to reference a file that is in the
".bat directory"\EmailList\list.txt
instead of
".jar directory"\EmailList\list.txt
and comes up with a fileNotFoundException.
Is there any way that I can run my batch file from a different directory while correctly referencing files in relation to the jar without hard coding in the jar's file path?
You can ask the JVM about the location of a given class. If you know it is inside the jar you refer to, then you can then extract the location of the jar file, and then construct your file objects relative to that.
See https://stackoverflow.com/a/320595/53897 for details about
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
This question already has answers here:
How to read file from relative path in Java project? java.io.File cannot find the path specified
(15 answers)
Closed 6 years ago.
I have exported a project to a runnable JAR in eclipse. There are a few places in my code where I've done the following.
String file = "src/Files/...";
loadFile(file); // purely for example
Now that the project is in the form of a JAR, those directories don't seem to exist and loading those files fails. I'm pretty sure that the files I'm referencing are packed in the JAR. Do the directories change in any particular way when exported to JAR? Any other ideas on how to make this work?
You need to treat them as a classpath resource, not as a local disk file system path. This isn't going to work when you package the files in a JAR and you also don't want to be dependent on the working directory. Files inside a JAR are part of the classpath already.
Assuming that you've a foo.txt file in package com.example, then you can get an InputStream of it as follows
InputStream input = getClass().getResourceAsStream("/com/example/foo.txt");
// ...
Or when you're inside static context
InputStream input = SomeClass.class.getResourceAsStream("/com/example/foo.txt");
// ...
Or when you want to scan the global classpath
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("/com/example/foo.txt");
// ...
See also:
How to read file from relative path in Java project? java.io.File cannot find the path specified
getResourceAsStream() vs FileInputStream