0

I have done a project with a simple Java Rest service and Ajax calls.

Unfortanely if i set the path to the json file as something general (fileName.json), it will not open my file.

If i set a complete path like C:\Users\Username\workspace\RestApplication\fileName.json, it works but when i will submit the project for review, it wont have the same path on my teachers computer.

The file currently resides in the main folder of the project. How can i make a general path that will work on whatever computer opens the project?

Thanks!

IvanSt
  • 360
  • 4
  • 17

2 Answers2

0

Although numerous different options you most likely want to refer to it on the classpath. See https://en.wikipedia.org/wiki/Classpath_(Java)

Peter
  • 5,556
  • 3
  • 23
  • 38
0

you should put the file you want to load in the classpath.

The class path is the path that the Java runtime environment searches for classes and other resource files.

Put your file inside your src or resources folder (I don't know your project structure)

and try to load it with:

InputStream is = TestResource.class.getResourceAsStream("/fileName.json");

or put the file under WEB-INF

@Context ServletContext servletContext;
InputStream is = servletContext.getResourceAsStream("/WEB-INF/fileName.json");
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29