Wrong classpath when Tomcat is launched in Idea



  • There's a problem in the launch. TomcatIdea

    If you start a test class, it works. The point is, it's not the way to the project file, it's "replaced" by Tomcat'u

    The path shall be:

    C:\Users\AntonioPrime\IdeaProjects\ProjectName\src\main\resources\ingredients.txt
    

    Code:

    Files.lines(Paths.get("src/main/resources/ingredients.txt").toAbsolutePath()).filter(e -> !(e.equals(""))).forEach(e -> {
                    Ingredient ingredient = constructIngredient(e);
                    ingredients.put(ingredient.getName(), ingredient);
                });
    

    Erroneous error for unclear reasons by other means:

    java.nio.file.NoSuchFileException: C:\apache-tomcat-8.5.4\bin\src\main\resources\ingredients.txt



  • The point is, you have a relative path, so when you start your IDE code, it's one, and when it's done in the context of Tomkat, the other.
    I understand this file is a resource in your project. To get a resource, use something like that. this.getClass().getResource("/ingredients.txt") or this.getClass().getResourceAsStream("/ingredients.txt")
    P.S. Your question has nothing to do with classpath Tomcat :




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2