getResourceAsStream() problem

F

Fencer

Hello, I have a problem with getResourceAsStream().

I'm using these tools: Eclipse JEE edition version 3.5.1 with the Apache
CXF plugin and Apache Tomcat version 6.0.20. Java is version 6. This is
under Windows.

I've written a very simple JAX-WS web service, here's the complete code:

package bioweb;

import java.rmi.Remote;
import java.rmi.RemoteException;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(name="BioWebInterface", targetNamespace="http://bioweb/")
public interface BioWebInterface extends Remote {
@WebMethod(operationName="sayHello", action="urn:SayHello")
public String sayHello() throws RemoteException;
}

package bioweb;

import java.io.InputStream;
import java.rmi.RemoteException;

import javax.annotation.PostConstruct;
import javax.jws.WebService;

@WebService(targetNamespace="http://bioweb/",
endpointInterface="bioweb.BioWebInterface", portName="BioWebPort",
serviceName="BioWebService")
public class BioWeb implements BioWebInterface {

@Override
public String sayHello() throws RemoteException {
return "Hello from the BioWeb Web Service!";
}


@PostConstruct
protected void init() {
System.out.println("I'm in the postconstruct method.");

InputStream is1 =
getClass().getClassLoader().getResourceAsStream("get-all-model-links.xquery");
InputStream is2 =
BioWeb.class.getClassLoader().getResourceAsStream("get-all-model-links.xquery");

if (is1 != null && is2 != null) {
System.out.println("Successfully loaded resource using both
methods!");
}
else if (is1 != null) {
System.out.println("Successfully loaded resource using method
1.");
}
else if (is2 != null) {
System.out.println("Successfully loaded resource using method
2.");
}
else {
System.err.println("Not successful at all in loading the
resource!");
}
}


/**
* @param args
*/
public static void main(String[] args) {
new BioWeb().init();
}
}

I've created folder called xqueries and added it to the classpath (under
project properties->java build path->libraries tab->add class folder
button and in it I have an XQuery file named get-all-model-links.xquery
(will be lots more later).

When I run the BioWeb class like a normal Java program (by invoking
main(), which calls init(), I can load the XQuery resource fine, the
output is:
I'm in the postconstruct method.
Successfully loaded resource using both methods!

However, when run as a service (then Tomcat calls the init() method due
to its @PostConstruct annotation), the loading of the resource fails.
The output is:
I'm in the postconstruct method.
Not successful at all in loading the resource!

It seems that the class folder I added isn't visible anymore or maybe
the "URL:s" need to be altered when in this context. I'm very new at
this web stuff and I need help, I've had this problem for two days now. :(
I'm doing all the interfacing with Tomcat via Eclipse btw, and I haven't
really tampered with any settings for it.

Thanks for reading and for any replies!

- Fencer
 
D

Daniel Pitts

Hello, I have a problem with getResourceAsStream().
[snip my OP]

I believe I solved this problem!
It is often considered courteous to explain how you solved your problem.

Others may find your post while looking into a similar problem, and
would benefit from your explanation.
 
L

Lew

Fencer said:
Hello, I have a problem with getResourceAsStream().

[snip my OP]

I believe I solved this problem!

Thank you so much for sharing the solution so that others besides
yourself may benefit!
 
R

Roedy Green

I believe I solved this problem!

Some day someone will have the same problem as you, find you question
in Google then curse you for refusing to share your solution.
 
F

Fencer

Hello, I have a problem with getResourceAsStream().
[snip my OP]

I believe I solved this problem!

- Fencer

Hello again. I wish to offer my sincere apologies for not specifying the
details of my solution in my previous post.

The problem turned out to be not in the actual Java code but in how I
was using my tools. Adding a directory to the classpath in the Eclipse
project settings didn't propagate to Tomcat in my context. What I did
was to add the directory to the classpath to Tomcat's launch
configuration under Eclipse. That solved it!

If you you doubleclick on the Tomcat server under the Server tab (in the
window that usually holds tabs like console, problems etc, I'm not sure
what it's called), an "Overview" of settings should open. There's a link
there "Open Launch Configuration" which opens a configuration dialog.
The dialog has a tab called "classpath" and you can add your entry
there, under "User Entries".

Again, I apologise for not writing this the first time around and thanks
to those who reminded me to do so.

- Fencer
 
L

Lew

Hello, I have a problem with getResourceAsStream().
[snip my OP]
I believe I solved this problem!

Hello again. I wish to offer my sincere apologies for not specifying the
details of my solution in my previous post.

The problem turned out to be not in the actual Java code but in how I
was using my tools. Adding a directory to the classpath in the Eclipse
project settings didn't propagate to Tomcat in my context. What I did
was to add the directory to the classpath to Tomcat's launch
configuration under Eclipse. That solved it!

If you you doubleclick on the Tomcat server under the Server tab (in the
window that usually holds tabs like console, problems etc, I'm not sure
what it's called), an "Overview" of settings should open. There's a link
there "Open Launch Configuration" which opens a configuration dialog.
The dialog has a tab called "classpath" and you can add your entry
there, under "User Entries".

You're supposed to include libraries needed by a web app in the "lib/"
directory of the web app.

You shouldn't entirely depend on the IDE to specify a class path. You
should be able to deploy the application from the command line.
 
F

Fencer

You're supposed to include libraries needed by a web app in the "lib/"
directory of the web app.

You shouldn't entirely depend on the IDE to specify a class path. You
should be able to deploy the application from the command line.

Thanks for your reply, Lew, and you make a dood point. There are several
jar-files in my WebContent->WEB-INF->lib folder, but if I put my XQuery
file in there, getResourceAsStream() doesn't find it :(

If I could make it work without adding a directory to classpath like I'm
doing that would be even better!

- Fencer
 
L

Lew

Fencer said:
Thanks for your reply, Lew, and you make a dood point. There are several
jar-files in my WebContent->WEB-INF->lib folder, but if I put my XQuery
file in there, getResourceAsStream() doesn't find it :(

If I could make it work without adding a directory to classpath like I'm
doing that would be even better!

There are already several directories in your class path for a web app.

Is your "XQuery file" a JAR? No? Then it doesn't go in the "lib/" directory.

The root directory of your application ("application-name/") and the classes
directory ("application-name/WEB-INF/classes/") are already in your path. Put
your resource relative to one of those.
 
R

Roedy Green

Again, I apologise for not writing this the first time around and thanks
to those who reminded me to do so.

Thank you for sharing your solution rather than getting defensive as a
normal person would.
 
F

Fencer

There are already several directories in your class path for a web
app.

Is your "XQuery file" a JAR? No? Then it doesn't go in the "lib/"
directory.

No, it's a text file containing code written in the XQuery (an XML query
language), I should have detailed that better in my OP. That's why I
didn't put it in the lib directory (however, I did put the Saxon
jar-file there which I use for running XQuery).
The root directory of your application ("application-name/") and the
classes directory ("application-name/WEB-INF/classes/") are already
in your path. Put your resource relative to one of those.

I can't check right now but I believe I already put the file in the
project root but I still couldn't load it. I will try again a little later.

Can I get runtime information on which directories are in the classpath?

- Fencer
 
M

Mike Schilling

Fencer said:
No, it's a text file containing code written in the XQuery (an XML
query language), I should have detailed that better in my OP. That's
why I didn't put it in the lib directory (however, I did put the
Saxon
jar-file there which I use for running XQuery).


I can't check right now but I believe I already put the file in the
project root but I still couldn't load it. I will try again a little
later.

I don't think the project root is in the classpath. The normal wasy
to make a resource available is

1. Put it below WEB-INF/classes, e.g. a resource named a/b/c.xml would
go at WEB-INF/classes/a/b/c.xml
2. Put it into a jar file, and put the jar file into WEB-INF/lib
 
L

Lew

Mike said:
I don't think the project root is in the classpath. The normal wasy
to make a resource available is

Depends on which of the three types of 'getResource*' methods you use. Those
in 'javax.servlet.ServletContext' include the project root.
 
M

Mike Schilling

Lew said:
Depends on which of the three types of 'getResource*' methods you
use. Those in 'javax.servlet.ServletContext' include the project
root.

That you need to call a method on a completely different class is a
critical bit of information to omit. And it remains true that the
project root is not in the classpath.
 
J

John B. Matthews

[QUOTE="Lew said:
I don't think the project root is in the classpath. The normal wasy
to make a resource available is

Depends on which of the three types of 'getResource*' methods you use. Those
in 'javax.servlet.ServletContext' include the project root.[/QUOTE]

In this context, does "three types" mean those "getResource*" methods
found in java.lang, java.beans.beancontext, and javax.servlet?
 
L

Lew

John said:
[QUOTE="Lew said:
I don't think the project root is in the classpath. The normal wasy
to make a resource available is
Depends on which of the three types of 'getResource*' methods you use. Those
in 'javax.servlet.ServletContext' include the project root.

In this context, does "three types" mean those "getResource*" methods
found in java.lang, java.beans.beancontext, and javax.servlet?[/QUOTE]

Four!

I had in mind Class, ClassLoader and ServletContext. Packages don't have methods.
 
J

John B. Matthews

No, but you know perfectly well what he means.

And answered what he intended to say. What's your problem?[/QUOTE]

I thank you both for clearing that up. I had tried asterisks as
wildcards, but the meaning was even less clear.

I was intrigued by how the three packages implement the notion of
getting a resource: ClassLoader is foundational, while the bean approach
"allows a BeanContext implementation to interpose behavior between the
child Component and underlying ClassLoader." The ServletContext
implementation "does not use class loaders" at all but serves a similar
purpose relative to the servlet container's context.

This seems like a handy pattern to put in one's hip pocket.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top