【问题标题】:Java Can't find file on ubuntuJava在ubuntu上找不到文件
【发布时间】:2021-08-11 03:00:02
【问题描述】:

我有问题。我有一个类,其函数名为getAgentStrategy。在该函数中,我有以下代码:

try(FileReader reader =  new FileReader("src/main/java/com/honda/strategies/agent_0001.config")) {
            
    // Read all properties from agent strategy file
    Properties properties = new Properties();
    properties.load(reader);

    // Assign all properties to variables
    String template = properties.getProperty("template");
    String market = properties.getProperty("market");
    String coin = properties.getProperty("coin");

    // Create strategy object with given values
    AgentStrategy agentStrategy = new AgentStrategy();

    agentStrategy.setTemplate(template);
    agentStrategy.setMarket(market);
    agentStrategy.setCoin(coin);

    return agentStrategy;
    
}
catch (Exception e) {;
    e.printStackTrace();
    return null;
}

此文件 (agent_0001.config) 确实存在于以下目录中:src/main/java/com/honda/strategies/。当我使用 maven 在 Windows 机器上的 VS Code 中运行此代码时,一切正常并且可以找到该文件。现在我已经在我的 Ubuntu 机器上安装了 Maven 并将我的项目复制到服务器上。该项目成功构建,但当我尝试运行它时,出现以下错误:

java.io.FileNotFoundException: src/main/java/com/honda/strategies/agent_0001.config (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:108)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)
    at com.hatop.drivers.StrategyDriver.getAgentStrategy(StrategyDriver.java:234)
    at com.hatop.drivers.StrategyDriver.run(StrategyDriver.java:60)
    at com.hatop.drivers.HatopDriver.lambda$4(HatopDriver.java:193)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at java.base/java.lang.Thread.run(Thread.java:832)

我不想硬编码项目的路径,因为我可以在多个环境中运行,并且此代码在使用 VS CodeIntelliJ 的 Windows 版本上运行。

这是因为我在 Ubuntu 上运行的版本是 .jar 文件,它没有我提供的文件树结构吗?

我该如何解决这个问题?

【问题讨论】:

标签: java maven ubuntu


【解决方案1】:

文件路径问题是我觉得最令人沮丧的错误之一,但 Maven 的 standard folder structure 包含一个 resources 文件夹,其内容可以通过任何类上可用的 getClass().getResource(String path) 函数访问。

└───maven-project
    ├───pom.xml
    └───src
        ├───main
        │   ├───java  // Put YourClass.java here
        │   ├───resources  // <instance of YourClass>.getClass().getResource("filename.ext") will find `filename.ext` saved here.

如果您的类在包内(例如org.example.agent),则getResource() 将从resources/org/example/agent/ 开始其文件搜索。

This Stack Overflow answer 详细解释了getResource() 的工作原理。

【讨论】:

  • 您应该建议以流的形式获取,否则当他们尝试使用此代码时,这也会中断。
  • getClass().getResource() 是常见的建议,但被巧妙地破坏了:当你有多个模块/库和子类发生时,就会破坏。正确的方法是MyClass.class.getResource,不管这种恶作剧如何,它总是有效的。当你有两种相同的方法来做一件事,其中一种在严格的超集场景中工作,那么永远不要使用另一种:这是毫无意义的学习曲线增加和风格的毫无意义的分歧。因此,即使在这里不相关,getClass() 仍然是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多