Reputation: 2913
I am trying to load Properties
from a file in a resource class of a jersey webapp. This is the resource and I followed the advice here
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("persons")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource {
public PersonResource()
{
Properties props = new Properties();
InputStream is = PersonResource.class.getClass().getClassLoader().getResourceAsStream("WEB-INF/test.txt");
if(is == null) { System.out.println("Inputstream is null");}
else
{
try
{
props.load(is);
System.out.println(props.propertyNames());
}
catch (IOException e) { e.printStackTrace(); }
}
}
@GET
public List<Person> getAllPersons()
{
List<Person> res = new ArrayList<>();
res.add(new Person("peter", "sullivan"));
res.add(new Person("claire", "wood"));
return res;
}
@GET
@Path("/{fn}")
public Person getPerson(@PathParam("fn") String fn)
{
return new Person("peter", "sullivan");
}
}
This is my folder structure
where the test.txt
is located in WebContent/WEB-INF/
. The file is just a single line "key=value". I added the WebContent
folder as a source folder.
When I run the app on tomcat 7, I get
java.lang.NullPointerException
demo.rest.jersey.resource.PersonResource.<init>(PersonResource.java:25)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.lang.reflect.Constructor.newInstance(Constructor.java:423)
org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1375)
org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:272)
org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:366)
org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)
Line 25 where the error occurs is InputStream is = PersonResource.class.getClass().getClassLoader().getResourceAsStream("WebContent/WEB-INF/test.txt");
Thanks for the help!
Upvotes: 0
Views: 295
Reputation: 61
The problem is around the path itself. it should be relative path. like you have to come out from the current folder and go to the folder where we have the test.txt.
OR
copy that test.txt in the folder where you have PersonResource .java and the path should be "test.txt"
Upvotes: 1
Reputation: 2913
The answer is here. This works now:
@Path("persons")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class PersonResource implements ServletContextListener
{
@GET
public List<Person> getAllPersons()
{
List<Person> res = new ArrayList<>();
res.add(new Person("peter", "sullivan"));
res.add(new Person("claire", "wood"));
return res;
}
@GET
@Path("/{fn}")
public Person getPerson(@PathParam("fn") String fn)
{
return new Person("peter", "sullivan");
}
@Override
public void contextDestroyed(ServletContextEvent ctxt) {
System.out.println("Destroying context");
}
@Override
public void contextInitialized(ServletContextEvent ctxt) {
System.out.println("PersonResource initialized!");
Properties props = new Properties();
try(InputStream is = ctxt.getServletContext().getResourceAsStream("/WEB-INF/test.txt"))
{
props.load(is);
System.out.println("key= " + props.getProperty("key"));
}
catch(Exception e)
{
System.out.println("Cannot load test.txt");
}
}
}
Upvotes: 0
Reputation: 61
if you are using eclipse, do the right click on the test.txt file, and press on the properties.
It opens a dialogue bow, there you will find the path of that file. Copy that path and run it.
Upvotes: 1