After reading up on some options (sqlite, derby etc...), I've decided to throw down with HSQLDB. I've downloaded it, read up on it and followed a 'hello world' type intro to it, and am now stuck.
I believe that you have to put the hsqldb.jar file in the src folder, so I did exactly that. Then I made a reference to the package with Eclipse by going into Run -> Run Configurations, then going into the Classpath tab, then clicking User Entries, then add External Jar, and selecting hsqldb.jar.
I get this :
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver
Here's my code :
package mysqlite;
import java.sql.*;
public class myclass {
public static void main(String[] args) {
try {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:db";
String user = "aUser";
String password = "";
Connection conn = DriverManager.getConnection(url, user, password);
}
catch(Exception e) {
System.out.println(e.toString());
}
}
}
I understand it's unable to find a class, but I thought that was what the hsqldb.jar provided.
No, you do not have to put it into src folder. src is for source files (*.java). You have to add this jar into your classpath: click on project properties, choose "Java build path", select tab "Libraries" and add the jar here.
The jar can be stored anywhere in your file system. Sometimes people create lib directory under project home and put all 3rd party dependencies there.
Try putting the .jar here:
<YOUR_JAVA_HOME>\jre\lib\ext
Related
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....
package aplikacjajava;
import java.sql.*;
public class main
{
public main( String args[] )
{
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:projekt.db");
} catch ( Exception e ) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
Error:
java.lang.ClassNotFoundException: org.sqlite.JDBC
I know that I'm missing JDBC driver. How to install it on Fedora?
I don't how to add some "path" and what is this at all. I need something like step-by-step explanation... This is first time when I met with such a problem.
Download the SQLite-jar at http://search.maven.org/remotecontent?filepath=org/xerial/sqlite-jdbc/3.8.10.1/sqlite-jdbc-3.8.10.1.jar
Add the jar file to your class path (open Eclipse project properties, there you'll find the build path)
Run your program.
You usually use Maven, Gradle or manual dependency management when using Java. Linux package managers are very limited in java support.
After I set the classpath from the command prompt, I could use the Database by running java javafile.java, and the database was working fine only from the command prompt.
The Eclipse IDE does not recognize the class. While I tried to add external jar file, the good news was when I defined the classpath as a variable inside the Eclipse IDE, then the issue was solved completely.
I did notice your request and tried to add well-detailed instructions on how to fix that issue.
For also solving this issue, follow the steps below:
Download a sqlite-jdbc-XXX.jar file
in the Eclipse IDE from the left side, right-click on your project and then select properties
on the left menu, choose the Java build path and click Add variable
click Configure variables and then click New
you should name the variable name field as you name it on the Class.forName("org.sqlite.JDBC"); line, that means your variable name is: org.sqlite.JDBC
on the variable path, you should choose your sqlite-jdbc-XXX.jar file
Finally, I got results from the database also in the Eclipse IDE.
I am attempting to deploy a swing application that uses a dll to facilitate a database connection (using sqljdbc4 jar that depends on sqljdbc_auth.dll). My deployment strategy is creating an executable jar file. The problem is that I cannot get the functions that rely on the sqljdbc_auth.dll to work in the executable jar I create.
I can get the project working fine in eclipse by using any of the following methods:
Reference the folder containing the dll in the build path configuration by specifying a Native library location on the Source folder
Put the dll in C:\Windows\System32 directory
Add the following VM argument referencing the relative path of the dll folder in my project "-Djava.library.path=path/to/dllfolder"
The 4th method I have tried is to load the dll directly in code via the following line. System.load("C:/Users/Me/Desktop/sqljdbc_auth.dll");
I have been unsuccessful with this approach and get the following error.
Mar 20, 2015 4:18:44 PM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
However, that error doesn't come directly from the line of code that loads the library, it seems to come afterwards when the sqljdbc library is trying to load the dll for use in making the connection.
Either way, I cannot get anything of the above methods working when I deploy the app as an executable jar file via the export functionality of eclipse. The only thing I can get is the previously mentioned error message.
The following sample application produces the same result.
public class TestApp {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
JFrame mainFrame = new JFrame();
mainFrame.setSize(300,300);
mainFrame.setLocationRelativeTo(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
try{
//System.load("C:/Users/Me/Desktop/sqljdbc_auth.dll");
String serverName = "local";
String databaseName = "test_database";
String connString = "jdbc:sqlserver://" + serverName + "; databaseName=" + databaseName + ";integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(connString);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select distinct name from test_table");
rs.next();
JOptionPane.showMessageDialog(null, "From db: " + rs.getString(1), "Test", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "Test", JOptionPane.INFORMATION_MESSAGE);
} catch (ClassNotFoundException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "Test", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.getMessage(), "Test", JOptionPane.INFORMATION_MESSAGE);
}
}
});
}
}
I've read tons of posts on using dlls with executable jars and they all seem to reference using this System.load method. Ironically, that is the one thing I can't get working. I know I have the right dll; however, because I can get other methods to work in the IDE environment. I don't care if the dll is packaged with the executable jar or not, I just want it to work!
I would presume this has something to do with putting the dll's in your library
(In Eclipse, Properties->Java Build Path->Libraries).
When you export the jar, you also will have the option of exporting the library files into a folder.
If you decompile the Jar after that, you'll notice that there's a manifest file, and in it, paths to where your library files are (based on the export, which created a library folder for you, generally jarname_lib).
When you export, you have the option to save as ANT file, which you can then edit to change the export location of the library files to a folder name of your choice. You can then add this edited ANT file to your build so that it happens whenever you build the project:
(In Eclipse, Properties->Builders->New)
I included the 3rd party jar file in the java project but still this is showing compile time error.
I included the path of the jar file in add external jar's in java.
package com.aamir;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;//compile time error occurs here.
public class ReadCSV3 {
public static void main(String[] args)
{
CSVReader reader = null;
try
{
//Get the CSVReader instance with specifying the delimiter to be used
reader = new CSVReader(new FileReader("SampleCSVFile.csv"),',');
String [] nextLine;
//Read one line at a time
while ((nextLine = reader.readNext()) != null)
{
for(String token : nextLine)
{
//Print all tokens
System.out.println(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Your problem is that you are including the source only JAR file. That's the problem right there. You have the sources only (uncompiled source) JAR file and not the binary JAR (it must contain the .class files). You need opencsv-2.2.jar instead.
Here is a zip file of the binary JAR.
I included the path of the jar file in add external jar's in java.
You need to add it to the project.
You didn't mention exactly how to added the jar. Can you please post the screenshot of
Right click project --> Build Path --> Configure --> Libararis (3rd Tab ) --> Add External Jar
You should add the jar here. Please do so if not already done.
What development tools are you using? Are you compiling at the command line or in an IDE?
If at the command line, you need to include the third party JAR in the CLASSPATH - either as an environment variable or as an argument to javac.
If in an IDE such as Eclipse, you need to specify the add the JAR to the build path in the project.
Firstly, just for sanity's sake, double-check that the JAR is in the build path - that you have given it the right name and the right path - you should see it in the Referenced Libraries section of your Java project if you expand it in the Project View. Try removing it and adding it again.
Then, secondly, check that the class you are importing exists - that you have got the right package name and class name (it is case sensitive). You should be able to expand the JAR in Eclipse and see the packages and classes inside it.
Thirdly, if you are certain that everything is correct, try to use Eclipse's auto-import feature - delete your import line and press CTRL-SHIFT-O (for Organise Imports) and see if it imports the right class.
If it is not auto-importing, then it indicates that the class is not in the class path. Try steps 1 and 2 again.
Also, try cleaning Project --> Clean --> Clean all projects
Can anyone please tell me as to how to setup the path for Connector/j using the "mysql-connector-java-5.1.18-bin.jar" in windows 7?
I'm using the below code and it always end up throwing an exception.
(java.lang.ClassNotFoundException : com.mysql.jdbc.driver)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver
{
public static void main(String[] args)
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Connection working");
}
catch (Exception ex)
{
System.out.println("Connection Fail");
System.out.println(ex.getMessage());
}
}
}
I tried following the official documentation of connector/j for setting up the path, but could not understand.
I tried adding E:\PROGRAM FILES\JAVA\jdk1.7.0_01\jre\lib\ext\mysql-connector-java-5.1.18-bin.jar in the "path" environment variable, Please correct me.
First, you dont need newInstance on Class.forName("com.mysql.jdbc.Driver")
Second don't copy jars to your JDK folder, there shouldn't ever bee a need to copy them there. The correct thing to do is add the jar to your project as a dependency. If your not using an IDE, then you want to add the jar to java.exe as a --classpath option when you run your code (run "java.exe /?" for more details). If you're using eclipse, you should add the mysql jar to the project by clicking on the project and selecting "properties" and then "Java Build Path" there will be an "Add JARs..." button on the right. Then the IDE will add it to your classpath automatically.