0

My project is created by Intellij, and this is a gradle project, path is: c:\project\

This is the program

Path path = Paths.get("input.txt");
List<String> lines=java.nio.file.Files.readAllLines(path, Charset.forName("utf-16le"));

I must use relative path, because I will use this command to run the program in future, and the input.txt

java -jar project.jar

The jar are generated at

C:\project\out\artifacts\project_jar\project.jar

So I copy input.txt to

C:\project\out\artifacts\project_jar\input.txt

But my program cannot found the input.txt if I run the program in intellij at development time

it only can read the file if I place the file at

C:\project\input.txt

I don't want to manually copy all dependent files to jar's path every time I release the program

How to make sure the file can be read in both situations?

CL So
  • 3,647
  • 10
  • 51
  • 95
  • "I don't want to copy all dependent files to jar path every time I release the program" That's good as doing this won't help. There is no built in support for reading a file from the same directory as a JAR. – Peter Lawrey Sep 12 '15 at 18:05
  • 1
    You can read a file if a) it's in the current working directory, b) if you provide the entire path or c) in the class path which for a JAR means in the JAR file. To do this for the last option you need to use `ClassLoader.getResourceAsInputStream()` – Peter Lawrey Sep 12 '15 at 18:08

0 Answers0