Re: solving paths on the jdbc archive (SOLVED)

Michel Maria-Sube <[email protected]> Thu, 18 Jun 2009 08:23:11 -0700 (PDT)
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>
The classpath must be also declared in the manifest, otherwise Class.forName() is unable to find complete path for the class
Works fine now

--- On Thu, 6/18/09, Federico Alcantara <[email protected]> wrote:

> From: Federico Alcantara <[email protected]>
> Subject: Re: solving paths on the jdbc archive
> To: "Michel Maria-Sube" <[email protected]>
> Date: Thursday, June 18, 2009, 2:07 AM
> Michel,
> can you post the file structure?
> Anyway, you can  have the mysql connector jar in the
> same directory as your jar. In such way your classpath
> prefix will be simple ./
> For example:
> 
>     yourarchive.jar
>        
> mysql-connector-java-5.1.6-bin.jar
> 
> To run your application (assuming Main.java is your
> application launcher class)
>     java -cp
> ./;./mysql-connector-java-5.1.6-bin -jar yourarchive.jar
> Main
> 
> Normally additional jars are put in a lib subdirectory, for
> example the folder structure can be as follow:
>      + yourarchive.jar
>      |
>      ---- lib
>           |
>           +
> mysql-connector-java-5.1.6-bin.jar
> Now your classpath will be prefixed with ./lib/
>     java -cp
> ./;./lib/mysql-connector-java-5.1.6-bin -jar yourarchive.jar
> Main
> 
> You can set your classpath with the values previously shown
> in the -cp option.
> 
> You must make sure that:
> 1) The jar are EXACTLY located in the directory that you
> are specified in the classpath, they are CASE sensitive.
> 2) That you have at least read/execute access to those
> directories.
> 
> Hope this helps!
> 
> 
> On Jun 16, 2009, at 6:15 PM, Michel Maria-Sube wrote:
> 
> > 
> > Thank you for this reply but I'm currently working
> under Linux with version 1.5.0_15 of the JDK, well maybe my
> classpath variable is not correctly set; it is set as:
> /usr/share/jdbc/mysql-connector-java-5.1.6-bin.jar:/usr/share/tomcat/apache-tomcat-6.0.18/lib/servlet-api.jar
> > 
> > Something is missing?? I don't know equivalent of
> \jarfiles\log4j-1.2.8.jar under Linux
> > 
> > Thank you for suggestions
> > Regards
> > Michel
> > 
> > On 4/19/09, Douglas Nelson <[email protected]>
> wrote:
> >> This is the classpath I set:
> >> 
> >>
> classpath=.;c:\;c:\download\java\commons-logging-1.0.4.jar;c:\jarfiles\log4j-1.2.8.jar;c:\jarfiles\mysql-connector-java-5.1.5-bin.jar;
> >> 
> >> this is the code I use to connect to a MySql
> database
> >>
> //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> >> // start getConnection
> >>
> //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> >>   /**
> >>    * Creates a new connection to the
> database
> >>    * @return the connection object
> >>    */
> >>   private Connection
> getConnection() throws Exception{
> >>      logger.info("getConnection
> called");
> >>      //------------------------
> >>      // Load up the jdbc driver
> >>      //------------------------
> >>      try {
> >>     
>    Class.forName("com.mysql.jdbc.Driver").newInstance();
> >>      } // end try
> >>      catch (Exception any) {
> >>     
>    logger.error("Unable to load driver. [" +
> any.getMessage() + "]");
> >>         throw(any);
> >>      } // end catch
> >> 
> >>     
> return(DriverManager.getConnection("jdbc:mysql://" +
> mysql_host +
> >> ":" + mysql_port +"/" + mysql_database + "?user="
> + mysql_user +
> >> "&password=" + mysql_password));
> >> 
> >>   } // end getConnection
> >> 
> >> 
> >> 
> >> Michel Maria-Sube wrote:
> >>> hello,
> >>> 
> >>> I'm working on an application connecting with
> mysql5, considering that
> >>> $CLASSPATH is correctly set and piece of code
> for connection is as
> >>> follows:
> >>> 
> >>>    public void connectDB( String
> DBname, String user, String passwd ) {
> >>>        java.sql.Connection
> conn = null;
> >>>        String   
> url = null;
> >>> 
> >>>        try {
> >>>           
> Class.forName("com.mysql.jdbc.Driver").newInstance();
> >>>        }
> >>>        catch
> (ClassNotFoundException cnfe) {
> >>>           
> System.out.println("\nClassNotFoundException occured");
> >>>           
> System.out.println("connection with the DB is not
> possible");
> >>>           
> cnfe.printStackTrace();
> >>>           
> System.exit(ERROR);
> >>>        }
> >>>        catch
> (InstantiationException ie) {
> >>>           
> System.out.println("InstantiationException occured");
> >>>           
> System.out.println("connection with the DB is not
> possible");
> >>>           
> System.exit(ERROR);
> >>>        }
> >>>        catch
> (IllegalAccessException iae) {
> >>>           
> System.out.println("IllegalAccessException occured");
> >>>           
> System.out.println("connection with the DB is not
> possible");
> >>>           
> System.exit(ERROR);
> >>>        }
> >>>        try {
> >>>            url =
> new
> >>>
> String("jdbc:mysql://localhost/"+DBname+"?user="+user+"&password="+passwd);
> >>> .......................
> >>> 
> >>> Execution gives following exception:
> >>> 
> >>> ClassNotFoundException occured
> >>> connection with the DB is not possible
> >>> java.lang.ClassNotFoundException:
> com.mysql.jdbc.Driver
> >>>        at
> java.net.URLClassLoader$1.run(URLClassLoader.java:200)
> >>>        at
> java.security.AccessController.doPrivileged(Native Method)
> >>>        at
> java.net.URLClassLoader.findClass(URLClassLoader.java:188)
> >>>        at
> java.lang.ClassLoader.loadClass(ClassLoader.java:306)
> >>>        at
> sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
> >>>        at
> java.lang.ClassLoader.loadClass(ClassLoader.java:251)
> >>>        at
> java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
> >>>        at
> java.lang.Class.forName0(Native Method)
> >>>        at
> java.lang.Class.forName(Class.java:169)
> >>>        at
> MainFrame.connectDB(MainFrame.java:290)
> >>>        at
> MainFrame.createMainScreen(MainFrame.java:260)
> >>>        at
> MainFrame.<init>(MainFrame.java:68)
> >>>        at
> MainFrame.main(MainFrame.java:1049)
> >>> 
> >>> This behavior disapears if I update the jar
> archive for executable with
> >>> classes content in the jdbc archive but of
> course I don't want to do that;
> >>> does anybody could indicate me why java is
> anable to solve correctly paths
> >>> in the jdbc archive?
> >>> 
> >>> Thank you in advance
> >>> Regards
> >>> M.
> > 
> > --MySQL Java Mailing List
> > For list archives: http://lists.mysql.com/java
> > To unsubscribe:    http://lists.mysql.com/[email protected]
> > 
> 
> Federico Alcantara
> [email protected]
> 
> 
> 
> 

-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]