Newbie - a question about import and .jar statement

Discussion in 'Java' started by zalek, Jul 26, 2008.

  1. zalek

    zalek Guest

    Hello,

    I wrote a java application that is using "import
    com.microsoft.sqlserver.jdbc.*;" , compiled it and created a .jar
    file.
    Program is working OK from my IDE.
    Here is my question:
    I want to execute this .jar file an a different PC. Do I need to put
    on this PC also a jar file with classes used for "import
    com.microsoft.sqlserver.jdbc.*"?

    Thanks,

    Zalek
    zalek, Jul 26, 2008
    #1
    1. Advertising

  2. zalek

    Arne Vajhøj Guest

    zalek wrote:
    > I wrote a java application that is using "import
    > com.microsoft.sqlserver.jdbc.*;" , compiled it and created a .jar
    > file.
    > Program is working OK from my IDE.
    > Here is my question:
    > I want to execute this .jar file an a different PC. Do I need to put
    > on this PC also a jar file with classes used for "import
    > com.microsoft.sqlserver.jdbc.*"?


    Yes. You need to put the MS jar file as well. Unless
    you extract the class files from the MS jar file and
    put them in your jar file, which would be a bad idea
    and may even violate license terms.

    Arne

    PS: You should not need to import the MS classes. JDBC
    should work with pure reflection and interfaces.
    Arne Vajhøj, Jul 26, 2008
    #2
    1. Advertising

  3. zalek

    zalek Guest

    On Jul 25, 10:38 pm, Arne Vajhøj <> wrote:
    > zalek wrote:
    > > I wrote a java application that is using "import
    > > com.microsoft.sqlserver.jdbc.*;" , compiled it and created a .jar
    > > file.
    > > Program is working OK from my IDE.
    > > Here is my question:
    > > I want to execute this .jar file an a different PC. Do I need to put
    > > on this PC also a jar file with classes used for "import
    > > com.microsoft.sqlserver.jdbc.*"?

    >
    > Yes. You need to put the MS jar file as well. Unless
    > you extract the class files from the MS jar file and
    > put them in your jar file, which would be a bad idea
    > and may even violate license terms.
    >
    > Arne
    >
    > PS: You should not need to import the MS classes. JDBC
    >      should work with pure reflection and interfaces.


    Thanks Arne for you answer.
    Now I have more questions:
    1. which import statement should I use instead of
    com.microsoft.sqlserver.jdbc.*;
    2. If I will move the .jar with MS or JDBC classes to the same
    directory as a jar with my application - do I need to add this
    directory to a classpath?

    Thanks,

    Zalek
    zalek, Jul 26, 2008
    #3
  4. zalek

    Arne Vajhøj Guest

    zalek wrote:
    > On Jul 25, 10:38 pm, Arne Vajhøj <> wrote:
    >> zalek wrote:
    >>> I wrote a java application that is using "import
    >>> com.microsoft.sqlserver.jdbc.*;" , compiled it and created a .jar
    >>> file.
    >>> Program is working OK from my IDE.
    >>> Here is my question:
    >>> I want to execute this .jar file an a different PC. Do I need to put
    >>> on this PC also a jar file with classes used for "import
    >>> com.microsoft.sqlserver.jdbc.*"?

    >> Yes. You need to put the MS jar file as well. Unless
    >> you extract the class files from the MS jar file and
    >> put them in your jar file, which would be a bad idea
    >> and may even violate license terms.


    >> PS: You should not need to import the MS classes. JDBC
    >> should work with pure reflection and interfaces.


    > Now I have more questions:
    > 1. which import statement should I use instead of
    > com.microsoft.sqlserver.jdbc.*;


    java.sql.*

    > 2. If I will move the .jar with MS or JDBC classes to the same
    > directory as a jar with my application - do I need to add this
    > directory to a classpath?


    Put a Class-Path directive in your jar file's manifest that
    point to the teh MS jar file.

    Arne
    Arne Vajhøj, Jul 26, 2008
    #4
  5. zalek

    Mark Space Guest

    zalek wrote:

    > Thanks Arne for you answer.
    > Now I have more questions:
    > 1. which import statement should I use instead of
    > com.microsoft.sqlserver.jdbc.*;


    Normally, none. Use "Class.forName()" and supply the driver name as a
    parameter to the script or application.


    <http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html>

    > 2. If I will move the .jar with MS or JDBC classes to the same
    > directory as a jar with my application - do I need to add this
    > directory to a classpath?


    Sort of. To use the classpath, you would add the directories that
    ..class files are in. Jar files however have to be added themselves.
    I.e., add "C:\mydir\subdir\msfiles.jar" to the classpath. The .jar file
    has to be named explicitly.

    But this doesn't matter if you are running from a Jar file. The
    classpath is ignored. Set the class path attribute in the Jar's
    manifest file.

    <http://java.sun.com/docs/books/tutorial/deployment/jar/downman.html>

    You could also use a custom class loader and set the location of the Jar
    or other resource files via a supplied parameter (property, config file,
    user input, etc.). That's a lot more advanced however.

    <http://java.sun.com/docs/books/tutorial/deployment/jar/jarclassloader.html>
    Mark Space, Jul 26, 2008
    #5
  6. "Mark Space" <> wrote in message
    news:8uIik.34009$...
    > zalek wrote:
    >
    >> Thanks Arne for you answer.
    >> Now I have more questions:
    >> 1. which import statement should I use instead of
    >> com.microsoft.sqlserver.jdbc.*;

    >
    > Normally, none. Use "Class.forName()" and supply the driver name as a
    > parameter to the script or application.
    >
    >
    > <http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html>
    >

    [ SNIP ]

    Other useful references are:

    1)
    http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/drivermanager.html
    2) http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/datasource.html

    Note that DataSource is preferred if you can use it. As the docs point out,
    the advantages of this over using DriverManager are (a) not hardcoding
    driver info, and (b) taking advantage of connection pooling and distributed
    transactions etc. Of course, as far as (a) is concerned the hardcoding for
    DriverManager is strings, not class imports, and these strings can be stored
    externally according to best practices. Note that Mark's reference to
    Class.forName() is in the context of DriverManager.

    Using one of these approaches your application code is blissfully unaware of
    driver specifics. It only needs to import java.sql.* for the "generic" stuff
    like Connection.

    AHS
    Arved Sandstrom, Jul 27, 2008
    #6
  7. zalek

    Arne Vajhøj Guest

    Arved Sandstrom wrote:
    > "Mark Space" <> wrote in message
    > news:8uIik.34009$...
    >> zalek wrote:
    >>> Now I have more questions:
    >>> 1. which import statement should I use instead of
    >>> com.microsoft.sqlserver.jdbc.*;

    >> Normally, none. Use "Class.forName()" and supply the driver name as a
    >> parameter to the script or application.
    >>
    >> <http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html>

    >
    > Other useful references are:
    >
    > 1)
    > http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/drivermanager.html
    > 2) http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/datasource.html
    >
    > Note that DataSource is preferred if you can use it. As the docs point out,
    > the advantages of this over using DriverManager are (a) not hardcoding
    > driver info, and (b) taking advantage of connection pooling and distributed
    > transactions etc. Of course, as far as (a) is concerned the hardcoding for


    DataSource is usually the right way, but it should be noted that:
    - driver does not need to be hardcoded with DriverManager, it can be
    made configurable as well
    - you do not get connection pooling and distributed just by using
    DataSource, some data sources (often those used in Java EE
    contexts) *can* provide those features

    Arne
    Arne Vajhøj, Jul 27, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    0
    Views:
    566
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,717
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,751
    Roedy Green
    Feb 14, 2006
  4. Arnold Peters
    Replies:
    0
    Views:
    656
    Arnold Peters
    Jan 5, 2005
  5. Stéphane Klein
    Replies:
    0
    Views:
    271
    Stéphane Klein
    Jul 5, 2011
Loading...

Share This Page