JAVA Runnable .JAR File incomplete execution. Eclipse IDE Runs to completion - java

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:
Load a properties file
Connect to a MSSQL Database based on that SQL File (using java.sql.Connection)
Queries some data.
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.");
}

Related

running Postgresql in Java [duplicate]

I am having some difficulty in making connectivity with Java and PostgreSQL Database.I have download the JDBC4 Postgresql Driver, Version 9.2-1002 driver and properly set the application ClassPath. My code is as under
import java.sql.*;
public class JavaPostGreSQLConnectivity
{
public static void main(String[] args)
{
DB db = new DB();
db.dbConnect("jdbc:postgresql://127.0.0.1:5432/TestDB", "postgres","pwd");
}
}
class DB
{
public DB() {}
public void dbConnect(String db_connect_string, String db_userid, String db_password)
{
try
{
Class.forName("org.postgresql.Driver");
Connection conn = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected");
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
Upon running I am getting the below error
Is it complaining about
Class.forName("org.postgresql.Driver");
If so then what will be the driver name? However, I followed this for my learning purpose.
However, If I do
C:\Program Files (x86)\Java\jdk1.7.0\bin>java -cp C:\Users\pos
tgresql-9.2-1002.jdbc4.jar; JavaPostGreSQLConnectivity
connected
It works.Why I need to explicitly mention the driver again when I have already placed it in the classpath properly? Is there any alternative way (I just want to put the JAR file in Classpath and the program should read from there)?
Thanks in advance
The driver name is OK. It is the same as mentioned in the official docs of the driver. Therefore the driver is just not in the classpath.
You say:
I [...] properly set the application ClassPath
On the other hand you start the program by just calling:
java JavaPostGreSQLConnectivity
In that case no PG driver is on the classpath. You have to add it by hand using someting like
java -cp postgresql-jdbc4.jar JavaPostGreSQLConnectivity
EDIT The question has been changed while typing, hence the duplication.
You added the jar only in you IDE. This helps the IDE to compile your code. If you start the program using you IDE then the IDE will also set the classpath for you. But if you don't start via the IDE then nobody knows the correct classpath and it has to be set by hand.
Your options are:
start always via IDE
make some batch script which hides the setting of the classpath (common solution)
set the CLASSPATH environment variable (does not scale with other Java applications)
make an "Executable Jar" and set the classpath there. (Search this site using that term).
put the jar into a place where the JVM picks it up automatically (e.g. in the lib/ext directory of the JRE). But polluting the JRE/JDK libs is the worst option.
Note: This is all basic Java knowledge and has nothing to do with PostgreSQL.

JavaFX8, SQLite(Embedded) exe, exec jar can't read/update the runtime database

I am creating a simple desktop application using JavaFx8 and SQLite and Eclipse Neon IDE. The Application works fine when launched to test from Eclipse. But If I create an executable jar or a Windows Exe file, it doesn't read/update the database.
Here's the code snippet used for creating and reading database.
private Connection loadPropertiesFileSQLite() throws SQLException, ClassNotFoundException {
try {
inputStream = new FileInputStream("databasesqlite.properties");
properties.load(inputStream);
Class.forName("org.sqlite.JDBC");
// database path, if it's new database, it will be created in the project folder
conSQLite = DriverManager.getConnection("jdbc:sqlite:Application.db");
System.out.println("Database Opened Successfully.");
} catch (Exception e) {
System.out.println("DDDD");
e.printStackTrace();
}
return conSQLite;
}
The connectors and jars I'm using are these:
The database is created in embedded mode when the user runs the application for the first time.
I have searched and tried scores of combinations from changing the location of database in DriverManager.getConnection("jdbc:sqlite:Application.db");
to many other things. Is there anything big that I'm missing here?
Are you using the properties file for nothing?
there is my config: sqlite-jdbc-3.7.15.jar
//auto commit on the connection.
conSQLite.setAutoCommit(true);
and check if do you have a permission at the file "Application.db".
The key is to use conSQLite = DriverManager.getConnection("jdbc:sqlite::resource:File:\\{some_path}\\Application.db");
instead of
conSQLite = DriverManager.getConnection("jdbc:sqlite:Application.db");
Now it works all fine.

how can i use derby database with the build .jar file?

i'm using java with the derby database.
When i start the connection of the "jdbc:derby"-database in netbeans and run my code,the program shows up and everything works fine.
By opening the built ".jar"-file, the program just shows up and close immediately right after that.
So how do i have to configurate netbeans that the build .jar file work with the derby database?
(i thought derby was file based, so that the build process must generate something like a database file)
If your program crashes during the execution of your jar file, there is probably an exception. If it works in Netbeans, the exception is probably caused by the path to your derby.jar.
The first thing I can think of is that you have incluced the derby.jar in your application using a relative path and you have not moved the derby.jar inside your project library.
Try to do the following:
1: copy the derby.jar to a directory (for example /lib) in your project folder
2: include the jar using relative path from your /lib folder
EDIT:
Here is a sample code of how to create your database when it is newly created:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection con = DriverManager.getConnection("jdbc:derby:C:\\Users\\TheReaver\\MyDB;create=true;user=thereaver;password=12345");
PreparedStatement st = con.prepareStatement("SELECT ID, NAME FROM APP.TABLE1");
try
{
ResultSet res = st.executeQuery();
}
catch(SQLException e) {
//if table not found, then create all tables
st.executeQuery("CREATE TABLE TABLE1(ID int, NAME varchar(20)))");
//execute all statements that create your database
//or execute an sql file that stores all your queries
}

Exported .jar when run on other computers give "No suitable driver found for jdbc"

I made a small and simple program using IntelliJ IDEA to update content on some new columns in a MSSQL database. I downloaded the external library sqljdbc4.jar and added this to Project Settings > Libraries, as well as making sure that the library .jar is added to the output layout. I have confirmed that the sqljdbc4.jar is included to the exported .jar-file by unpacking the file on the Mac I use.
The program runs just fine on my Mac, but when I copy the .jar over to a colleague's computer, or try to run in in Windows on Parallels, we only get a ClassNotFound Exception with a following note that "No suitable driver found for jdbc:sqlserver[server details]"
The code for the job is as following:
//[....]
private final static String SQL_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
public SQLConnector(){
}
public boolean updateDatabase(String[] results, boolean inhouse){
Connection conn;
try{
Class.forName(SQL_DRIVER);
} catch (ClassNotFoundException e){
JOptionPane.showMessageDialog(null, "ERROR: " + e.getMessage(), "Exception type: ClassNotFoundException", JOptionPane.ERROR_MESSAGE);
return false;
}
//[...]
The ClassNotFound-exception is what is triggered. Any ideas? If some additional information is required, please do not hesitate to request it.
EDIT: I have the sqljdbc4.jar saved in /Library/Java/Extensions, and if I remove it from there then the program also fails with the same error on my Mac. Even if I have the .jar-file stored in a /bin/-folder in my project and make sure that this file is the file added as a library. With jdbc-jar removed from Extensions, the program will still run fine when executed from IntelliJ even so.
I figured it out. The problem was in the settings for artifacts. When specifying details to create JAR from modules, I had previously selected "extract to the target JAR", but the correct option seemed to be "copy to the output directory and link via manifest". Doing this gave an error that a manifest already existed, so I had to go manually in and delete the META-INF folder in both "src" and in "out > production > [project name]". Then allow IntelliJ to build the artifact anew. This resulted in the sqljdbc4.jar file being added alongside my own JAR, instead of copying the sqljdbc4.jar file into my own JAR.
I do not know what the major difference between these two options are however....

Sqlite database ..java.lang.ClassNotFoundException:org.sqlite.jdbc

Hello guys I have create a small java application for desktop that simple logs data into an sqllite database. The jar files work well on the computer that was built, but, when I distributed on other pc it display the message that goes like this java.lang.ClassNotFoundException:org.sqlite.jdbc
I used this code: I know I have to change the classpath apparently, but I am not sure how to proceed so that this program work on other pcs. I notice that this programs works well on the pc that it was built on but it is because i used this classpath..."jdbc:sqlite:C:\Users\USUARIO\Documents\workspace\School2015.sqlite"... which is my local computer.
My question is how do I change this classpath so that the program runs fine on other pc than the one it was built on?
public class sqlConnection {
Connection conn=null;
public static Connection dbConnector()
{
try{
Class.forName("org.sqlite.JDBC");
Connection conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\USUARIO\\Documents\\workspace\\School2015.sqlite");
JOptionPane.showMessageDialog(null, "BIENVENIDO! Estás Conectado");
return conn;
}catch (Exception e)
{
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Not sure how you package and/or run this but if you place the School2015.sqlite file inside of the built JAR file you are creating and reference it from the relative path instead of the absolute one you will have no problems sharing the app.
You could look into using Maven as a build tool that will allow you to package everything up.

Categories

Resources