简体   繁体   English

从命令行运行Java Eclipse项目

[英]Running a Java Eclipse Project from Command line

Hi I wanted to run a java Eclipse project form command line.. I tried making a Jar and Runnable Jar (Both options), with the manifest when needed. 嗨,我想运行java Eclipse项目表单命令行。我尝试制作一个Jar和Runnable Jar(两个选项),并在需要时使用清单。

The problem which I cannot solve is that it always ends up giving me a FileNotFound error when My program tried to read a txt file. 我无法解决的问题是,当我的程序尝试读取txt文件时,它总是最终给我FileNotFound错误。 How do I solve this error? 我该如何解决这个错误? I cannot find it online. 我无法在线找到它。

Thanks 谢谢

Example: 例:

in = new BufferedReader(new FileReader("CompanyNameListModified.txt"));

Error: 错误:

Exception in thread "main" java.io.FileNotFoundException: CompanyNameListModified.txt (No such file or directory)

Assuming that the text file is in the jar itself, then don't grab it as a File, since technically, these don't exist in jar files, but rather get it as a resource. 假设文本文件在jar本身中,则不要将其作为File抓取,因为从技术上讲,这些文件不存在于jar文件中,而是将其作为资源来获取。

eg, 例如,

package foo;

import java.io.InputStream;
import java.util.Scanner;

public class TestResource {
   public static void main(String[] args) {
      ClassLoader classLoader = ClassLoader.getSystemClassLoader();     
      String resourceName = "foo/movies.txt"; // path relative to classloader
      InputStream inStream = classLoader.getResourceAsStream(resourceName);
      Scanner scanner = new Scanner(inStream);
      while (scanner.hasNextLine()) {
         System.out.println(scanner.nextLine());
      }
      if (scanner != null) {
         scanner.close();
      }
   }   
}

the contents of the jar file are: jar文件的内容为:

C:\Users\hovercraft\Jar files>jar tf TestResource.jar
META-INF/MANIFEST.MF
foo/TestResource.class
foo/TestResource.java
foo/movies.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM