0

This is the actual error I'm getting:

NoInitialContextException: Need to specify class name in environment or system property

I'm trying to have a datasource connection working on a project without servlet but just a main to launch.

This is the main class:

package pacchetto;

import java.sql.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class testIt {

    public static void main(String[] args) throws SQLException, NamingException {

        Context ctx = new InitialContext();
        javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
        Connection con = ds.getConnection();

        try {
            con = ds.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null)
            System.out.println("Connessione riuscita");
        }
    }
}

In the same project I also have a servlet file and using it everything works fine. This led me to thinking that the problem must be in this class and not in the servlet.xml or any other configuration file. Should I be wrong and should you need the whole scene I'll be happy to paste them here along with the main.

What am I doing wrong?

pedro
  • 417
  • 2
  • 7
  • 25
  • 1
    You're assuming that a code which works in a servlet environment will work in a non-servlet environment. In addition to specifying the class of `InitialContext`, you'd need something to actually put the datasource there. Now you're assuming it magically appears at `java:comp/env/jdbc/datasource`. – Kayaman May 22 '17 at 08:44
  • So what kind of adjustment should I do? I've never set a no servlet datasource so I wouldn't know where to look. Once configured server.xml, context.xml and web.xml I thought I was fine.. what am I missing? – pedro May 22 '17 at 08:48
  • 1
    You configured server.xml, context.xml and web.xml, but you're running the application from a `main()`? – Kayaman May 22 '17 at 08:50
  • 1
    Possible duplicate of [How do I manually configure a DataSource in Java?](http://stackoverflow.com/questions/1336885/how-do-i-manually-configure-a-datasource-in-java) – Arnaud May 22 '17 at 08:52
  • Those three were already set for the servlet, so for example the GlobalNamingResources is ok. Yes, I'm running it from a main() class because it's an example I've found, but there's no way to make it work.. – pedro May 22 '17 at 08:53
  • @Berger I peaked that question, is it really about Datasource? Becase by looking at the answer doesn't seem so – pedro May 22 '17 at 08:55
  • 1
    @pedro, have a look at the other answers to this question, many are about `DataSource` . – Arnaud May 22 '17 at 09:00
  • Ok so, correct me if I'm wrong, but manually configuring a DataSource kinda miss the point of the DataSource itself.. I think I'm just gonna let this example slip because it keeps looking more and more pointless. Thank you both anyway! @Berger your link is what actually drove me to understanding the whole scene, I don't know if thats a thing but I'd be glad to select yours as the solution for this question! – pedro May 22 '17 at 09:26
  • 1
    Yes sure, in your case using a `DataSource` in your `main` has no special advantages, unless you want connection pooling and such. If the linked answer somehow matches your question, feel free to accept it (don't know how you do that, though) or upvote it . – Arnaud May 22 '17 at 09:29
  • You cold repost your link or add the answer and then the link and I'm going to be able to select it. – pedro May 22 '17 at 09:30

0 Answers0