Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

java - JNDI configuration for Springboot standalone JAR application

I created with Springboot a JAR application. I launch this JAR application under Tomcat server.

One asks me to use JNDI to retrieve datasource stored in the Tomcat context.xml, saying it's easy.

I try to do this with this following code.

Context initContext;
        try {
            initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:comp/env");
            DataSource ds = (DataSource) envContext.lookup("jdbc/mydatabase");
        } catch (NamingException e) {
            System.out.println("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            e.printStackTrace();
            System.out.println("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }

But it does not work.

I obtain this error :

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

Is it a simple mean to overcome this problem ?

Or with my JAR standalone application, it is still absolutely impossible to overcome this problem, like the precedent post Tomcat JNDI + Standalone Java informs ?

I see many posts, many docs but it remains unclear for me.

Thanks for your help, your sharing.

Thomas


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Kedar brings me an answer. I make a summary of the Kedar answer :

  • when running your JAR application with the jar command line, i am using embedded Tomcat
  • when i use embedded Tomcat, it's possible that i have not access to my JNDI datasource That's the reason why my JNDI does not work. I will try to see this post to configure my application in order to try tu use JNDI : How to create JNDI context in Spring Boot with Embedded Tomcat Container But, now, thanks to Kedar, i understand why my JNDI code does not work. I will see the other post to see how configure to use anyway JNDI if possible. Thanks Kedar.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...