Results 1 to 13 of 13
  1. #1
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Java .jar With SQL

    Ok lets see,

    I made a java app that gets data from the user.
    First name, last name, ect....
    all in a GUI. The user clicks on the button to submit the data is sent to SQL and it works fine.
    Note that the app is executed from cmd.

    Now I want to run the app from a .jar But when I do this. The app comes up on the screen
    the user inputs data and clicks send, But no data is sent to SQL.

    How do I fix this.

  2. #2
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: Java .jar With SQL

    Code debugging. System.out.println() is your friend.

  3. #3
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java .jar With SQL

    Ya that is true when running the app with cmd. But not when it is a .jar file.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Java .jar With SQL

    Just run it from the command line using java -jar then.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: Java .jar With SQL

    Quote Originally Posted by Adam
    Now I want to run the app from a .jar But when I do this. The app comes up on the screen
    the user inputs data and clicks send, But no data is sent to SQL.
    Just out of curiousity, how did you run it from command line?

    Quote Originally Posted by Adam View Post
    Ya that is true when running the app with cmd. But not when it is a .jar file.
    Whether it's a jar, war, exe or whatever, if you have system output in your code, it will always print to your system console/terminal if it can reach that portion of code. As Tolls, said, run it from command line.

  6. #6
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java .jar With SQL

    Ok I can run the app from cmd. All works well. Took the class files and made a manifest.txt file, and made the .jar file from all the class files. Only then when I open the app from the .jar file it does not send data to SQL. This is what im am not understanding. Why it works one way but not the other.

    Running the app from cmd is easy.
    first compile the app

    c:\user\desktop\TestSQLApp\ javac TestSQL.java

    Then run it
    c:\user\desktop\TestSQLApp\ java TestSQL

  7. #7
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java .jar With SQL

    Here is step by step on what i have done

    1. Made my java code in two file a main class, and the other class file that has all the methods.
    2. Compiled my java code, and ran it.
    3. Tested to make sure the data info was sent to MySQL, checked the data table in MySQL and the data was sent.
    so the class path is set right, and my app works
    4. Made a manifest.txt file, made a .jar file from the app class files. The .jar file works when double clicked on,
    entered data into the app clicked send. Checked MySQL table to confirm the data was sent, but nothing was.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Java .jar With SQL

    I assume, since you haven't got a -cp switch on that 'java' call, that you have set the CLASSPATH environment variable.
    Don't do that.
    That is why you are getting this problem with one and not the other.

    I am also assuming that, since you don't mention any errors when you run the jar file from the command line, that you are also eating exceptions.
    Don't do that.
    At the minimum (until you have a reasonable logging strategy in place) use printStackTrace().

    What is happening is that an executable jar file uses its own classpath, which is given in the manifest.
    That classpath should be relative to the jar file, usually a lib directory on the same level as the jar file containing all dependent jars.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java .jar With SQL

    yes i set the classpath environment variable. But I did not run the jar file from the cmd, just double clicked on it.
    Also I do have a printstacktrace()

    catch (Exception e)
    {
    e.printStackTrace();
    }

    So how do I go about making the classpath relative to the jar file on the same level as the jar file containing all dependent jars?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Java .jar With SQL

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java .jar With SQL

    Ok I some what understand. When I make the manifest I need to add a Class-Path:
    but as for the class path do I add the .jar file from MySQL connector.

    mysql-connector-java-5.1.24-bin.jar

    And do I move this .jar file to the directory where I have my app?

    Also when I make the manifest how does it start out?

    Main-Class: main
    Class-Path: mysql-connector-java-5.1.24-bin.jar

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Java .jar With SQL

    Yes to both.
    The class path in the manifest has to contain every jar file that your application requires in order to run.
    The usual thing is to put the dependent jar files into a lib directory next to the executable jar, but that's not a requirement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Adam is offline Member
    Join Date
    Apr 2013
    Posts
    7
    Rep Power
    0

    Default Re: Java .jar With SQL

    Oh Thank you so much for your time. I have been pulling my hair out over this. I have read book after book on this and getting nothing out of them. Looked all over the net and still nothing. Again thank you for your time on this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •