A. Vreeswijk
A. Vreeswijk

Reputation: 954

Java Can't find file on ubuntu

I have a problem. I have a class with a function called getAgentStrategy. In that function I have the following code:

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/. When I run this code in VS Code on my windows machine using maven, everything works fine and it can find the file. Now I have installed Maven on my Ubuntu machine and copied my project to the server. 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.

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?

How can I fix this?

Upvotes: 0

Views: 294

Answers (1)

thehale
thehale

Reputation: 1756

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-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 (e.g. org.example.agent), then getResource() will start its file search from resources/org/example/agent/.

This Stack Overflow answer explains more about how getResource() works.

Upvotes: 2

Related Questions