1

I am trying to build a single jar file for my project with all other libraries included (database drivers, etc). The build process works fine and I am able to create the jar. I am using Netbeans IDE for the same.

My problem: When I run the jar file directly by double clicking it in windows 7 or Ubuntu, jdbc (sqlite) driver doesn't work. Although when I run it from command line using java -jar myproject.jar, it works fine. Also it is working fine in Windows 8 and Mac OS by double clicking.

Here's the portion of build.xml that I use for making a single jar:

<target name="-post-jar">
<property name="store.jar.name" value="myproject"/>
<property name="store.dir" value="dist"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
    <zipgroupfileset dir="dist" includes="*.jar"/>
    <zipgroupfileset dir="dist/lib" includes="*.jar"/>
    <manifest>
        <attribute name="Main-Class" value="${main.class}"/>
    </manifest>
</jar>
<zip destfile="${store.jar}">
    <zipfileset src="${store.dir}/temp_final.jar"
    excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
</target>

Also interestingly, all other libraries (like jcalender) that I've used work well, except sqlite-jdbc. Any thoughts on why this may be happening?

Note : I also don't get any exceptions, ClassNotFoundException or such.

Edits :

  1. When I use a print statement (by which I mean writing to a file) before and after the jdbc connection statement, nothing happens in win 7/ubuntu.

  2. I tried using the same version of jre as that of Mac OS and Win 8 (i.e. jre 1.8) on ubuntu, thinking that maybe that was causing some problems. But nope, no luck there.

My database connection code :

        Class.forName("org.sqlite.JDBC");            
        SQLiteConfig config = new SQLiteConfig();  
        config.enforceForeignKeys(true);  
        System.out.println(config.toProperties());
        ErrorLog.write_to_file("fetching connection");            
        Connection conn = DriverManager.getConnection("jdbc:sqlite:Data.db",config.toProperties());
        Statement stmt=conn.createStatement();
        ErrorLog.write_to_file("connected");

The ErrorLog class writes errors to a file. Also please note that this portion is in a try-catch block. In the catch block, I again use ErrorLog to write to file

I've put the database file in the same folder as the jar file.

dg131
  • 156
  • 8
  • `"When I run the jar file directly by double clicking it, jdbc (sqlite) driver doesn't work."` What isn't working? – Tim Biegeleisen Jul 11 '15 at 09:27
  • I have a table in my application which gets populated from the database when the application starts.So the table is empty if I run the jar directly. – dg131 Jul 11 '15 at 09:28
  • Have you tried putting a print statement before and after the database write? What is happening exactly when you double click the JAR? I doubt that it is being executed. – Tim Biegeleisen Jul 11 '15 at 09:30
  • Yes I tried doing that. My connection is being established alright. – dg131 Jul 11 '15 at 09:45
  • If you can connect then how could a write fail without any error in the Java log or SQLite log? – Tim Biegeleisen Jul 11 '15 at 09:49
  • That is exactly what I dont understand. I should also mention that it works fine on my laptop (which is where I built the application, win 8) but no where else. I thought it was a classpath problem. But then I realized it wasn't, since all other libraries were working fine. – dg131 Jul 11 '15 at 09:52
  • Compare the run properties between your laptop and desktop. – Tim Biegeleisen Jul 11 '15 at 09:57
  • 1
    Yes. They are the same. I am trying to work upon the problem by printing statements to my error log file. – dg131 Jul 11 '15 at 10:13
  • @TimBiegeleisen I tried a lot of cases. And I am sorry I was wrong in saying that db connection is fine. The database isn't being connected in other laptops (win 7 and ubuntu). I dont get any print statements there. Its working on win 8, mac os but not on ubuntu or windows 7. – dg131 Jul 11 '15 at 13:00
  • Bingo. Either rewrite your question title or open a new question. I upvoted your question. – Tim Biegeleisen Jul 11 '15 at 13:02
  • Done. Thank you for your response. – dg131 Jul 11 '15 at 16:16
  • Sounds like you might be swallowing an exception somewhere. And/or opening a database with a relative path. Could you [edit] your question to show the code that connects to the database? – Dan Getz Jul 11 '15 at 16:44
  • Sure, done. Have a look. – dg131 Jul 11 '15 at 16:57
  • Do your know for a fact that sqlite can actually find your db file? Or that the native component is being loaded correctly? Have you tried giving an absolute path to the db file? What about using a memory db temporarily to avoid any path issues? – pvg Jul 11 '15 at 17:10
  • Yes it can. How else would you explain it running correctly from the `java jar myproject.jar` command? – dg131 Jul 11 '15 at 17:14
  • On the command line, you have a well-defined working directory. – pvg Jul 11 '15 at 18:12
  • Double clicking on windows launches the jar with a working directory of the windows system directory. You have to [use an answer like this](http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file) to determine the origin directory and use that for the db open and the error log file open so that they write to the correct place. – Anya Shenanigans Jul 11 '15 at 20:36
  • If it really makes sense to use a default file name and path for your database file, then I'd suggest you put it somewhere in the user's home directory. See http://stackoverflow.com/q/585534/3004881 – Dan Getz Jul 13 '15 at 03:05

0 Answers0