0

Running into an issue with a JAVA application that I'm hoping someone can provide some clarity to. I have a simple Java Application which is being built in the Eclipse IDE. I have JRE 1.8.0_251 installed on PATH.

My application at a basic level performs the following:

  1. Load a properties file
  2. Connect to a MSSQL Database based on that SQL File (using java.sql.Connection)
  3. Queries some data.
  4. Updates some data.

This all works without issue in Eclipse. However, when I export my file to a runnable .JAR file, it appears to stop after attempting to initialize the database connection. However, it does not throw a SQLException and never returns from the method. I checked my .jar file in 7ZIP to make sure that the SQLJDBC42.jar was packaged in the folder and it was.

The last log line I see when my runnable JAR executes is: Log.info("Connecting to Database...") however, all items run / and are logged in Eclipse execution.

Any ideas? Code below:

 public void ConnectDatabase() { 
                    
        Log.info("Connecting to Database...");
        
        try {
            //Connect to Database and Log success.
            ConnectionInstance = DriverManager.getConnection(connectionURL);
            Log.info("Database Connection Successful.");
            
        } catch (Exception e) {
            Log.info(String.format("ERROR | DATABASE: %s",  e));
            //System.exit(0);
        }             
        
        Log.info("Leaving Database Method.");
        
    } 

sallou
  • 99
  • 9
  • How are you including and using the dependencies in the jar? Via Maven? Home-grown classloading from resources? – Dave Newton Jul 22 '20 at 22:42
  • Hey Dave, Thanks for the response. I'm not 100% sure what your asking as I'm coming over from a .NET background but I assume it works the same as .dll files. I added the dependent .JAR files in Eclipse by navigating to Project -> Properties -> Java Build Path -> Add Class Folder and adding them there. I assumed since I selected "Package required libraries into Generated JAR" Upon export in Eclipse it would include those and reference them correctly. Am I missing a step perhaps? – sallou Jul 22 '20 at 22:46
  • When running java apps from command line make sure you specify `-cp path/to/jars` and remember that the shell directory the comamnd is run from becomes the current directory of the app. – Bohemian Jul 22 '20 at 23:14
  • Oh, I missed you we’re building an eclipse jar with dependencies—it takes care of the class loading part for you. – Dave Newton Jul 22 '20 at 23:31
  • @Bohemian - Thank you for the suggestion. Unfortunately that did not resolve the issue. Quick question though, in Java if I create a .jar with all external jars built in via Eclipse do I still need to run as CP or will it know its in the same root path? – sallou Jul 23 '20 at 14:29
  • No, you don’t need to worry about CP. Just `java -jar myApp.jar`. Is your properties file either in the jar or in the directory you run it from? – Bohemian Jul 23 '20 at 14:45

0 Answers0