简体   繁体   English

Java 在 ubuntu 上找不到文件

[英]Java Can't find file on ubuntu

I have a problem.我有个问题。 I have a class with a function called getAgentStrategy .我有一个 class 和一个名为 getAgentStrategy 的getAgentStrategy In that function I have the following code:在那个 function 我有以下代码:

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;
}

This file ( agent_0001.config ) does exist in the directory: src/main/java/com/honda/strategies/ .此文件 ( agent_0001.config ) 确实存在于以下目录中: src/main/java/com/honda/strategies/ When I run this code in VS Code on my windows machine using maven, everything works fine and it can find the file.当我在我的 windows 机器上使用 maven 在 VS Code 中运行此代码时,一切正常,它可以找到该文件。 Now I have installed Maven on my Ubuntu machine and copied my project to the server.现在我已经在我的 Ubuntu 机器上安装了 Maven 并将我的项目复制到服务器上。 The project gets successfully build, but when I try to run it, I get the following error:该项目已成功构建,但是当我尝试运行它时,出现以下错误:

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)

I don't want to have the path of the project hard coded, because I can run on multiple environments and this code does work on a windows version using VS Code or IntelliJ .我不想对项目的路径进行硬编码,因为我可以在多个环境中运行,并且此代码确实适用于使用VS CodeIntelliJ的 windows 版本。

Is this caused by the fact that the version I am running on Ubuntu is a .jar file which doesn't have the filetree structure I provided?这是因为我在 Ubuntu 上运行的版本是.jar文件,它没有我提供的文件树结构吗?

How can I fix this?我怎样才能解决这个问题?

File path issues are one of the bugs I find most frustrating, but Maven's standard folder structure includes a resources folder whose contents can be accessed by the getClass().getResource(String path) function available on any class.文件路径问题是我发现最令人沮丧的错误之一,但 Maven 的标准文件夹结构包括一个resources文件夹,其内容可以通过getClass().getResource(String path) function 在任何 class 上访问。

└───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.

If your class is inside of a package (eg org.example.agent ), then getResource() will start its file search from resources/org/example/agent/ .如果您的 class 位于 package 内部(例如org.example.agent ),则getResource()将从resources/org/example/agent/开始其文件搜索。

This Stack Overflow answer explains more about how getResource() works.这个 Stack Overflow 答案解释了有关getResource()工作原理的更多信息。

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

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