Title: How can I make a custom resource also be lookuped in glassfish by the java:com/env-scheme?
Tags: java;jakarta-ee;glassfish
Question: I've created some JDBC resources and Custom resources in GlassFish. I named JDBC resource jdbc/mydb and the Custom resource service/test.
The JDBC connection is lookedup with the call ic.lookup("java:comp/env/jdbc/mydb"). It can also be found with ic.lookup("jdbc/mydb"), which has some disadvantages, see answer of Robin below.
But my custom resource has to be lookedup with ic.lookup("service/test"). The line ic.lookup("java:comp/env/service/test") does not work. Is it possible, and if yes: how?


Here is another answer: I am not sure what Glassfish means by 'custom resource'. The Java EE specification says that the 'java:comp/env' namespace is reserved for resource lookups which are bound only for that particular application. So
ic.lookup("java:comp/env/jdbc/mydb")
actually means that jdbc/mydb is a name binding for the application (EJB/servlet etc) that you are doing the lookup from. Doing a direct lookup without using the java:comp/env prefix ties down your application to the server environment that you are deploying on.

Here is another answer: If you created your custom-resources in your domain.xml, maybe you forgot to add them as a resource-ref under the servers/server tag in your domain.xml:
<resources&gt;
<!-- JDBC RESOURCE --&gt;
...
<custom-resource res-type="java.lang.String" jndi-name="MyCustomStringResource"
factory-class="org.glassfish.resources.custom.factory.PrimitivesAndStringFactory"&gt;
<property name="value" value="hello, this is a string resource"&gt;</property&gt;
</custom-resource&gt;
...
</resources&gt;
<servers&gt;
<server name="server" config-ref="server-config"&gt;
<application-ref ref="__admingui" virtual-servers="__asadmin"&gt;</application-ref&gt;
...
<resource-ref ref="MyCustomStringResource"&gt;</resource-ref&gt;
...
</server&gt;
</servers&gt;

Here is another answer: The prefix indicates the usage of a resource reference which has been defined for the Java EE entity from which the JNDI lookup is being made.  You would have to define such a reference for you custom resource as well to look it up the same way.
The prefix is not necessary for the JDBC lookup either, as you can use the full jndi name instead.  The problem with that approach, is if you move the resource such that it is no longer located in the local initial context, you will not find it without changing the lookup name in your code to include the location information.  Using a reference shields your code from this, so only the mapping in the reference would change.
Some info here on usage
Comment for this answer: No, it isn't, but the principles for how it is used are.  The configuration examples are specific to ibm and the specific protocols being used find resources.  Not sure if this can be vendor neutral, you would have to determine how to do it with your app server of choice.
Comment for this answer: OK. But what do I need to define, so I can use the 'java:com:env/'-prefix for custom resources as well? I didn't do somehtings special for the JDBC, so GlassFish/NetBeans must be providing for this. I only added resources in my web.config and resource reference in sun-web.xml.

Here is another answer: glassfish 3 and up uses an admin gui on localhost:4848
use the gui and go to JDNI >Custom Resources>New Resource
for previous version of glassfish i would recommend changing the glassfish.xml to instantiate a new resource using the  specifiers

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:03/11/09', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><jakarta-ee><glassfish>', 'Title': 'How can I make a custom resource also be lookuped in glassfish by the java:com/env-scheme?', 'ViewCount': 4820, 'id': 634598, 'user_AboutMe': "<p>I'm both a Mac and Windows user. I like programming web applications. My languages of choice are Python, Javascript and C#.</p>\n", 'user_AccountId': 45.0, 'user_DisplayName': 'doekman', 'user_Reputation': 18292.0, 'user_Views': 943.0, 'user_id': 56.0}}