public class RedCompiler {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("help.txt"));
Lexer lexer = new Lexer();
Parser parser = new Parser(lexer);
parser.start();
}
运行后出现以下内容,该文件无法建立和读取,希望您能为我提供帮助...
Exception in thread "main" java.io.FileNotFoundException: help.txt (El sistema no puede encontrar el archivo especificado)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at redcompiler.RedCompiler.main(RedCompiler.java:26)
C:\Users\itzel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
help.txt
文件放置在项目中的位置,也没有说明项目的运行方式(命令行与NetBeans中的对比),因此无法精确诊断问题。但是仍然可以提出解决方案。首先,请勿执行main()
中的处理,因为您将无法调用获取有关应用程序的运行时详细信息所需的非静态方法getClass()
。
第二,为了使代码在NetBeans中运行项目时正常工作,可以将getClass()
直接放在项目的根目录下,然后将其找到。但是,如果您随后尝试从命令行运行项目的jar文件,则会得到help.txt
,因为jar文件中不会包含FileNotFoundException
。
因此,请直接在现有软件包的help.txt
文件夹下,为resources
文件创建一个help.txt
文件夹。例如,如果您的项目在src
下有一个名为redcompiler
的包,则项目结构的一部分应如下所示:
src
这将确保src redcompiler RedCompiler.java resources help.txt
将包含在您的jar文件中。然后,在help.txt
方法中,您可以编写在NetBeans中运行以及从命令行运行项目的jar文件时将成功访问main()
的代码。但是,尽管有多种方法可以执行此操作,但使用help.txt
并不是OP中所示的一种。有关更多详细信息,请参见对FileInputStream
的接受的答案。相反,您可以使用How can I access a txt file in a jar with FileInputStream?读取
help.txt
,您可以使用通过BufferedReader
或InputStream
获得的ClassLoader.getResourceAsStream()
实例化它。这是使用两种方法读取Class.getResource()
的示例代码:
help.txt
这是在NetBeans中运行项目时的输出:
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()); } }
这是从命令行运行项目的jar文件时的输出:
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)
注意:
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>
调用记录在java