cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing resources in EJB

Former Member
0 Kudos

Hello,

I have an stateless session bean, and I need to access some resources which I packaged in the resulting jar.

For example I have source package like this:

org.example.bean.*

org.example.files.*

In the code I thought that I will be able to access these files in common way:

String urlName = "org/example/files/file1"

URL resURL = this.getClass().getResource(urlName);

or

URL resURL = ClassLoader.getSystemResource(urlName);

but it don't work, everytime it returns null.

Maybe in EJB container the code like that dont function, I don't know but it's working in common java aplication so I thought it will be so also in ejb.

Can anyone tell me how can I access my files?

Any help would be appreciated.

Best Regards,

Miroslav Koskar

Accepted Solutions (0)

Answers (2)

Answers (2)

deepa_prabhu
Participant
0 Kudos

Hi Miroslav ,

Are you trying to access stateless session bean or classes

from a packaged jar into your stateless session bean?

If you are trying to access the stateless session bean,then you can go by the way Ray has suggested.

Or, if you are trying to access the classes inside a jar in you stateless session bean then here u go...

In your EJB->project->right clicj-> properties->java build path -> Add External Jars and add the desired jars.

Now you will be able to access it as you wanted.

Hope this suggestion helps.

Pl reward points if the suggestion helped you.

Thanks and regards

Deepa

Former Member
0 Kudos

Hi Ray, Deepa

My problem is not in accessing EJB from second EJB. I want to write in ejb bussiness method the code, in which I am accessing a set of files which are packaged in jar (EJB module). Lets imagine that these files are a 10 images and the bussiness method have to read a specified of them and then return it in own serialization implementation of image class to client. Now is my problem that I even cant load the image from file in EJB because I'm getting null URL object in above code.

If you have an idea what I'm doing wrong, please post.

Best regards,

Miroslav Koskar

deepa_prabhu
Participant
0 Kudos

Hi Miroslav,

Can you tell me whether your jar contains image files

or java classes representing these image files? Can you

please explain me your entire scenario in a nutshell?

Regards

Deepa

Vlado
Advisor
Advisor
0 Kudos

Hi Miroslav,

Have you tried with

String urlName = "<b>/</b>org/example/files/file1"
URL resURL = this.getClass().getResource(urlName);

This is described in the documentation of the Class.getResource(String name) method.

Hope that helps!

Vladimir

Former Member
0 Kudos

Hi

Please check the restriction of EJB components.

can you use

this

keyword?

alternatively you can write helper class and invoking it in EJB is good idea i guess.

Regards

Former Member
0 Kudos

To access methods in your bean from another program, you need to use JNDI to lookup the bean. On help.sap.com, you can search for "Enterprise Bean Clients" for more info. Also, here's some code that might help.

import java.rmi.RemoteException;

import java.util.*;

import javax.ejb.CreateException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

Context ctx = new InitialContext(props);

Object o = ctx.lookup("mycomp.com/TheEAR/TheBean");

home = (ReportHome)

PortableRemoteObject.narrow(o, TheHome.class);

bean = home.create();

String myString = bean.myMethod();

The above assumes the name of the bean is TheBean and also assumes you a public method named myMethod in the bean that returns a String.

Ray