You don't state where you have placed the help.txt
file within your project, nor how the project was run (command line vs. within NetBeans), so it is not possible to precisely diagnose your problem. But it is still possible to propose a solution.
First, do not perform the processing in main()
since you will not be able to invoke the non-static method getClass()
which is needed to get runtime details about your application.
Second, to simply get the code to work when running your project within NetBeans, you could place help.txt
directly under the root of your project, and it would be found. But if you then tried to run the project's jar file from the command line you would get a FileNotFoundException
because help.txt
would not have been included in the jar file.
So instead, create a resources
folder for the help.txt
file directly under your existing package under the src
folder. For example, if your project had a package named redcompiler
under src
then part of your project structure would look like this:
src
redcompiler
RedCompiler.java
resources
help.txt
This ensures that help.txt
will be included in your jar file. Then, in your main()
method, you can write code that will successfully access help.txt
when you run within NetBeans, and also when running your project's jar file from the command line. However, while there are multiple approaches to do that, using a FileInputStream
, as shown in the OP is not one of them. See the accepted answer to How can I access a txt file in a jar with FileInputStream? for more details.
Instead, you could read help.txt
using a BufferedReader
, which you could instantiate using an InputStream
obtained by ClassLoader.getResourceAsStream()
or Class.getResource()
. Here is sample code that uses both approaches to read help.txt
:
package redcompiler;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
public class RedCompiler {
public static void main(String[] args) throws IOException, URISyntaxException {
new RedCompiler().demo();
}
void demo() throws IOException, URISyntaxException {
// Read file using ClassLoader.getResourceAsStream()
String path = "redcompiler/resources/help.txt";
InputStream in = getClass().getClassLoader().getResourceAsStream(path);
System.out.println("Using ClassLoader.getResourceAsStream(): path=" + path + ", InputStream=" + in);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
System.out.println("Using ClassLoader.getResourceAsStream(): " + reader.readLine() + '\n');
// Read file using Class.getResource()
path = "resources/help.txt";
URL url = getClass().getResource(path);
System.out.println("Using Class.getResource(): path=" + path + ", URL=" + url);
reader = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("Using Class.getResource(): " + reader.readLine());
}
}
This is the output when running the project within NetBeans:
run:
Using ClassLoader.getResourceAsStream(): path=redcompiler/resources/help.txt, InputStream=java.io.BufferedInputStream@15db9742
Using ClassLoader.getResourceAsStream(): This is the content of file help.txt.
Using Class.getResource(): path=resources/help.txt, URL=file:/D:/NB112/RedCompiler/build/classes/redcompiler/resources/help.txt
Using Class.getResource(): This is the content of file help.txt.
BUILD SUCCESSFUL (total time: 0 seconds)
And this is the output when running the project's jar file from the command line:
Microsoft Windows [Version 10.0.18363.476]
(c) 2019 Microsoft Corporation. All rights reserved.
C:\Users\johndoe>C:\Java\jdk1.8.0_221/bin/java -jar "D:\NB112\RedCompiler\dist\RedCompiler.jar"
Using ClassLoader.getResourceAsStream(): path=redcompiler/resources/help.txt, InputStream=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@55f96302
Using ClassLoader.getResourceAsStream(): This is the content of file help.txt.
Using Class.getResource(): path=resources/help.txt, URL=jar:file:/D:/NB112/RedCompiler/dist/RedCompiler.jar!/redcompiler/resources/help.txt
Using Class.getResource(): This is the content of file help.txt.
C:\Users\johndoe>
Notes:
- I created the project in NetBeans using File > New Project... > Java with Ant > Java Application.
- The
java
call to be made from the command line is logged in the Output window when you build your project.
- This is the structure of the project, as shown in the Files panel in NetBeans:
