Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem making a runnable .jar

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem making a runnable .jar

    Hello! I am having a problem. So I need to make my program a runnable .jar file. The problem is my SQLite database path.

    When I do this line of code the .jar program works fine, but only on my machine: conn = DriverManager.getConnection("jdbc:sqlite:/home/user/eclipse-workspace/LibrarySystem-master/LibrarySystem-master2/SE-Lib-System-master2/SELibSys2/LibraryDB2.db");

    The default obviously isnt working: conn = DriverManager.getConnection("jdbc:sqlite:LibraryDB 2.db");

    My connection to the database is in a separate class, in case that matters. I have tried many things. This gives me an error: "jdbc:sqlite::resource:LibraryDB2.db"

    Also, which runnable jar option should I pick in eclipse? Extract libraries to generated JAR, package libraries in generated JAR, or copy libraries into sub folder? I dont think I want the sub-folder one - this might be doing something I have been selecting package libraries in generated JAR.

    Getting errors like this: [SQLITE_ERROR] SQL error or missing database (no such table: Users) - It has to be something with the connection to the database because like I said it works fine on my end with the absolute path but on others machines its not working. I do understand why this is happening but I thought that when I created the runnable jar in eclipse it would pick up the database automatically.

    import java.sql.*;
    import javax.swing.*;
     
    // Below is the connection class that is used to connect to the database
     
    public class SqlConnection {
     
        public static Connection dbConnect() {
            Connection conn = null;
            try {
                Class.forName("org.sqlite.JDBC");
     
                // The database is located in this file path
                conn = DriverManager.getConnection("jdbc:sqlite:LibraryDB2.db");
                return conn;
            } 
            catch(Exception e) {
                JOptionPane.showMessageDialog(null, e);
                return null;
            }
        }
     
     
    }

    What can I do about this? The database is in the root and also the same folder as the source code.

    Thanks.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem making a runnable .jar

    The default obviously isnt working:
    How did you test it?
    Were the jar file and and .db file in the same folder?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem making a runnable .jar

    OK well, no the .jar and db were not in the same folder because I need it to be a stand alone application.

    The program compiles and generates the .jar fine with the "default" getConnection arguement, but when I go to do something that involves the database it errors out: "org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: Users)"
    SO I tested it by trying to use the app.

    Wait - Do you mean were the SQLite .jar file and .db in the same folder? Or MY generated .jar file?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem making a runnable .jar

    the .jar and db were not in the same folder because I need it to be a stand alone application.
    How will the program find the db if the db is not in a known location(it need not be the same folder).
    One way would be to pass the db address to the program as a commandline argument.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2020
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem making a runnable .jar

    I think in an earlier program I was able to do it with this: jdbc:sqlite::resource:LibraryDB2.db (which is erroring now) unless I really did have the db there or something. Sorry, I have no idea what you mean about using the command line for that purpose.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem making a runnable .jar

    How do you expect the program to find the db?
    One way is to have a standard location for it (for example in the same folder with the jar file)
    Another way is to pass the path on the command line.
    In a Windows OS most programs are started using a commandline. I don't know about unix but I suspect its similiar.
    For example here is the contents of the commandline when I compile a program:
    C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe -cp . -Xlint -Xdiags:verbose ViewThumbnails.java
    Here is an example of how to call a jar file and pass args to the program using the commandline:
    "C:\Program Files (x86)\Java\jre1.8.0_171\bin\java.exe" -jar BridgeSolver.jar THE args HERE
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to use data from obtainMessage in Runnable?
    By iceman999 in forum Android Development
    Replies: 0
    Last Post: February 8th, 2013, 05:23 AM
  2. Replies: 1
    Last Post: August 4th, 2012, 10:02 AM
  3. Problem of Making Jar
    By cppcppcpp in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2012, 11:54 AM
  4. creating runnable JAR file
    By olimpicco in forum Java IDEs
    Replies: 25
    Last Post: January 11th, 2012, 09:15 AM
  5. Runnable to WebService
    By fran_jo in forum Threads
    Replies: 1
    Last Post: April 12th, 2011, 04:12 PM