I'm trying to set up a simple Java application to connect to a SQLite database but keep receiving:
ClassNotFoundException: org.sqlite.jdbc.
I've downloaded the sqlite jdbc driver jar and placed it in the same directory as the .java, and I'm compiling on the command line with:
javac -cp sqlite-jdbc-3.7.2.jar sqlite3test.java
At runtime I then get the above exception. Below is the code:
import java.sql.*;
public class sqlite3test
{
public static void main(String args[])
{
Connection c = null;
try
{
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:cs261.db");
}
catch ( Exception e )
{
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
How do I fix this issue?
Thanks.
Check the database version installed, in my case it was: SQLite version 3.8.2
I am using sqlite-jdbc-3.8.9.1.jar which is the greater version number than of the database installed and it is working perfectly.
You have to make sure the jar file version should be greater than or equal to the database version installed in your system.
From the latest version of java (from Java 6+) you don't need to load your class file.
Remove the following line and try it should work.
Class.forName("org.sqlite.JDBC");
Note: I suggest you to rename your class name from sqlite3test to Sqlite3Test though its not mandatory. According to the JAVA code convention class name should always start with Caps letter.
There are different ways to load the driver into your classpath:
place the jar into the $JAVA_HOME/jre/lib/ext folder
using maven dependencies
using java -cp
if you are using the IDE like eclipse then right click the project -> build path -> configure build path -> libraries -> add external jars and locate your jar there.
For your reference : http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/
I can get working directory of current Java program using this code:
Path path = Paths.get(*ClassName*.class.getProtectionDomain().getCodeSource().getLocation().toURI());
Also I can get CommandLine parameters (but there is no directory in the output) of running Java processes using this command wmic process get CommandLine where name='java.exe' /value
It is possible to get working directory of another Java process (better programmatically)? Probably it can be solved with some jdk/bin utilities?
You can get this information via the Attach API. To use it, you have to add the tools.jar of your jdk to your class path. Then, the following code will print the current working directories of all recognized JVM processes:
for(VirtualMachineDescriptor d: VirtualMachine.list()) {
System.out.println(d.id()+"\t"+d.displayName());
try {
VirtualMachine vm = VirtualMachine.attach(d);
try(Closeable c = vm::detach) {
System.out.println("\tcurrent dir: "+vm.getSystemProperties().get("user.dir"));
}
}
catch(AttachNotSupportedException|IOException ex) {
System.out.println("\t"+ex);
}
}
I have a Java EE Web Application which connects to a SQL Server 2008 instance. I don't have any problem connecting and retrieving to all my tables, except for one of them. The error in the Tomcat log is:
WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in java.library.path
1) Download the JDBC Driver here.
2) unzip the file and go to sqljdbc_version\fra\auth\x86 or \x64
3) copy the sqljdbc_auth.dll to C:\Program Files\Java\jre_Version\bin
4) Finally restart eclipse
Here are the steps if you want to do this from Eclipse :
1) Create a folder 'sqlauth' in your C: drive, and copy the dll file sqljdbc_auth.dll to the folder
1) Go to Run> Run Configurations
2) Choose the 'Arguments' tab for your class
3) Add the below code in VM arguments:
-Djava.library.path="C:\\sqlauth"
4) Hit 'Apply' and click 'Run'
Feel free to try other methods .
For easy fix follow these steps:
goto: https://learn.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url#Connectingintegrated
Download the JDBC file and extract to your preferred location
open the auth folder matching your OS x64 or x86
copy sqljdbc_auth.dll file
paste in: C:\Program Files\Java\jdk_version\bin
restart either eclipse or netbeans
The error is clear, isn't it?
You've not added the path where sqljdbc_auth.dll is present. Find out in the system where the DLL is and add that to your classpath.
And if that also doesn't work, add the folder where the DLL is present (I'm assuming \Microsoft SQL Server JDBC Driver 3.0\sqljdbc_3.0\enu\auth\x86) to your PATH variable.
Again if you're going via ant or cmd you have to explicitly mention the path using -Djava.library.path=[path to MS_SQL_AUTH_DLL]
I've just encountered the same problem but within my own application.
I didn't like the solution with copying the dll since it's not very convenient so I did some research and came up with the following programmatic solution.
Basically, before doing any connections to SQL server, you have to add the sqljdbc_auth.dll to path.. which is easy to say:
PathHelper.appendToPath("C:\\sqljdbc_6.2\\enu\\auth\\x64");
once you know how to do it:
import java.lang.reflect.Field;
public class PathHelper {
public static void appendToPath(String dir){
String path = System.getProperty("java.library.path");
path = dir + ";" + path;
System.setProperty("java.library.path", path);
try {
final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths");
sysPathsField.setAccessible(true);
sysPathsField.set(null, null);
}
catch (Exception ex){
throw new RuntimeException(ex);
}
}
}
Now integration authentication works like a charm :).
Credits to https://stackoverflow.com/a/21730111/1734640 for letting me figure this out.
I had to use windows authentication and I tried every suggested solution out there but with no success till I changed the name of the auth file as follows:
old-name: mssql-jdbc_auth-10.2.0.x64.dll
new-name: sqljdbc_auth.dll
And then it worked!
I looked up for this case to understand it more, apparently for Windows operating systems, the driver looks for sqljdbc_auth.dll by default.
Here is a useful link I've found:
https://learn.microsoft.com/en-us/sql/connect/jdbc/feature-dependencies-of-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
To resolve I did the following:
Copied sqljdbc_auth.dll into dir: C:\Windows\System32
Restarted my application
I resolved the issue by:
Upgrading com.microsoft.sqlserver from 6.1.0.jre8 to 10.1.0.jre8-preview:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.1.0.jre8-preview</version>
</dependency>
Adding missing dependency:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
Using the following jdbc connection:
jdbc:sqlserver://;authenticationScheme=NTLM;integratedSecurity=true;domain=;databasename=;encrypt=true;trustServerCertificate=true;user=;password=
I'm developing a Grails web application and I need to use a JNI native library to access some specific hardware. For a simple Java application (see below) it works fine. To do this I just have to add the JAR to the Java build path and specify the "Native library location" (I use SpringSource Tool Suite on Windows7).
Working example:
import conceptacid.nativedriver.Driver;
public class Main {
public static void main(String[] args) {
System.out.println("starting...");
System.loadLibrary("AudioCardDriver");
Driver driver = Driver.instance();
String driverDescription = driver.getDriverDescription();
System.out.println( "Native application driver: " + driverDescription);
}
}
However, when I try to add it into my Grails application it fails:
Bootstrap.groovy:
import conceptacid.nativedriver.Driver;
class BootStrap {
def init = { servletContext ->
System.loadLibrary("AudioCardDriver");
Driver driver = Driver.instance();
String driverDescription = driver.getDriverDescription();
System.out.println( "Native application driver: " + driverDescription);
}
def destroy = {
}
}
the first line System.loadLibrary("AudioCardDriver"); is executed silently without any exception but the next line where I try to use my native code Driver driver = Driver.instance(); fails:
Running script C:\grails\scripts\RunApp.groovy
Environment set to development
[groovyc] Compiling 1 source file to D:\Projects3\mbr\target\classes
[delete] Deleting directory C:\Users\VShmyrev\.grails\1.3.7\projects\mbr\tomcat
Running Grails application..
2012-02-24 15:19:49,690 [main] ERROR context.GrailsContextLoader - Error executing bootstraps: java.lang.UnsatisfiedLinkError: conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.UnsatisfiedLinkError: conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)
...
Caused by: java.lang.UnsatisfiedLinkError: conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init()V
at conceptacid.nativedriver.AudioCardDriverJNI.swig_module_init(Native Method)
at conceptacid.nativedriver.AudioCardDriverJNI.<clinit>(AudioCardDriverJNI.java:70)
at conceptacid.nativedriver.Driver.instance(Driver.java:35)
at conceptacid.nativedriver.Driver$instance.call(Unknown Source)
at BootStrap$_closure1.doCall(BootStrap.groovy:7)
... 26 more
Application context shutting down...
I'm sure I put the DLL into a directory which is in my system PATH but it doesn't help.
What is the right way to use a native library in Grails application both in a development environment and in production?
Your DLL needs to be on a path specified in the Java system property java.library.path. On Windows the PATH environment variable and Linux the LD_LIBRARY_PATH environment variable are added to this system property. You can try logging the java.library.path system property to see if Java is looking in the right place for your DLL.
I'm guessing that the native library is being loaded multiple times in different classloaders, so forking a new JVM when running the app might help.
This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 7 years ago.
I am having trouble compiling and running my Java code, intended to allow me to interface Java with a shared object for Vensim, a simulation modeling package.
The following code compiles without error:
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
However, when I try to run the following:
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
I get the following error: "Error: Could not find or load main class SpatialModel
". My SpatialModel.java code does contain a 'main' method (below), so I'm not sure what the problem is - can anyone please help me out? Thanks.
import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
public class SpatialModel {
private VensimHelper vh;
public static final String DLL_LIBNAME_PARAM = "vensim_lib_nam";
public static final String MODEL_PATH_PARAM = "vensim_model_path";
private final static int VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT = 10;
public SpatialModel() throws SpatialException {
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if(libName == null || libName.trim().equals("")) {
log.error("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
throw new SpatialException("Vensim library name has to be set with -D" + DLL_LIBNAME_PARAM);
}
if(modelPath == null || modelPath.trim().equals("")) {
log.error("Model path has to set with -D" + MODEL_PATH_PARAM);
throw new SpatialException("Model path ahs to be set with -D" + MODEL_PATH_PARAM);
}
for (int i = 0; i < VENSIM_CONTEXT_CREATION_MAX_FAILURE_COUNT && vh == null; i++) {
try {
log.info("creating new vensim helper\n\tdll lib: " + libName + "\n\tmodel path: " + modelPath);
vh = new VensimHelper(libName, modelPath);
} catch (Throwable e) {
log.error("An exception was thrown when initializing Vensim, try: " + i, e);
}
}
if (vh == null) {
throw new SpatialException("Can't initialize Vensim");
}
}
public static void main(String[] args) throws VensimException {
long before = System.currentTimeMillis();
String libName = System.getProperty(DLL_LIBNAME_PARAM);
String modelPath = System.getProperty(MODEL_PATH_PARAM);
if (libName == null) {
libName = "libvensim";
}
if(modelPath == null) {
modelPath = "~/BassModel.vmf";
}
System.setProperty(DLL_LIBNAME_PARAM, libName);
System.setProperty(MODEL_PATH_PARAM, modelPath);
if (args.length > 0 && args[0].equals("info")) {
System.out.println(new VensimHelper(libName, modelPath).getVensimInfo());
} else if (args.length > 0 && args[0].equals("vars")) {
VensimHelper helper = new VensimHelper(libName, modelPath);
String[] vars = helper.getVariables();
for (String var : vars) {
System.out.println(helper.getVariableInfo(var));
}
} else {
File f = new File(".");
System.out.println(f.getAbsolutePath());
SpatialModel sm = new SpatialModel();
}
System.out.println("Execution time: " + (System.currentTimeMillis() - before));
}
}
You must ensure that you add the location of your .class file to your classpath. So, if its in the current folder, add . to your classpath.
Note that the Windows classpath separator is a semi-colon, i.e. a ;.
If the class is in a package
package thepackagename;
public class TheClassName {
public static final void main(String[] cmd_lineParams) {
System.out.println("Hello World!");
}
}
Then calling:
java -classpath . TheClassName
results in Error: Could not find or load main class TheClassName. This is because it must be called with its fully-qualified name:
java -classpath . thepackagename.TheClassName
And this thepackagename directory must exist in the classpath. In this example, ., meaning the current directory, is the entirety of classpath. Therefore this particular example must be called from the directory in which thepackagename exists.
To be clear, the name of this class is not TheClassName, It's thepackagename.TheClassName. Attempting to execute TheClassName does not work, because no class having that name exists. Not on the current classpath anyway.
Finally, note that the compiled (.class) version is executed, not the source code (.java) version. Hence “CLASSPATH.”
You can try these two when you are getting the error: 'could not find or load main class'
If your class file is saved in following directory with HelloWorld program name
d:\sample
java -cp d:\sample HelloWorld
java -cp . HelloWorld
I believe you need to add the current directory to the Java classpath
java -cp .:./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel vars
You have to include classpath to your javac and java commands
javac -cp . PackageName/*.java
java -cp . PackageName/ClassName_Having_main
suppose you have the following
Package Named: com.test
Class Name: Hello (Having main)
file is located inside "src/com/test/Hello.java"
from outside directory:
$ cd src
$ javac -cp . com/test/*.java
$ java -cp . com/test/Hello
In windows the same thing will be working too, I already tried
If you work in Eclipse, just make a cleanup (project\clean.. clean all projects) of the project.
You have to set the classpath if you get the error:
Could not find or load main class XYZ
For example:
E:\>set path="c:\programfiles\Java\jdk1.7.0_17\bin"
E:\>set classpath=%classpath%;.;
E:\>javac XYZ.java
E:\>java XYZ
I got this error because I was trying to run
javac HelloWorld.java && java HelloWorld.class
when I should have removed .class:
javac HelloWorld.java && java HelloWorld
Check your BuildPath, it could be that you are referencing a library that does not exist anymore.
If you're getting this error and you are using Maven to build your Jars, then there is a good chance that you simply do not have your Java classes in src/main/java/.
In my case I created my project in Eclipse which defaults to src (rather than src/main/java/.
So I ended up with something like mypackage.morepackage.myclass and a directory structure looking like src/mypackage/morepackage/myclass, which inherently has nothing wrong. But when you run mvn clean install it will look for src/main/java/mypackage/morepackage/myclass. It will not find the class but it won't error either. So it will successfully build and you when you run your outputted Jar the result is:
Error: Could not find or load main class mypackage.morepackage.myclass
Because it simply never included your class in the packaged Jar.
I know this question was tagged with linux, but on windows, you might need to separate your cp args with a ; instead of a :.
java -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar;./vensim.jar SpatialModel vars
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
If you try to run a java application which needs JDK 1.6 and you are trying to run on JDK 1.4, you will come across this error. In general, trying to run a Java application on old JRE may fail. Try installing new JRE/JDK.
Problem is not about your main function. Check out for
javac -d . -cp ./apache-log4j-1.2.16/log4j-1.2.16.jar:./vensim.jar SpatialModel.java VensimHelper.java VensimException.java VensimContextRepository.java
output and run it.
Project > Clean and then make sure BuildPath > Libraries has the correct Library.
java -verbose:class HelloWorld might help you understand which classes are being loaded.
Also, as mentioned before, remember to call the full qualified name (i.e. include package).
I was using Java 1.8, and this error suddenly occurred when I pressed "Build and clean" in NetBeans. I switched for a brief moment to 1.7 again, clicked OK, re-opened properties and switched back to 1.8, and everything worked perfectly.
I hope I can help someone out with this, as these errors can be quite time-consuming.
This problem occurred for me when I imported an existing project into eclipse. What happens is it copied all the files not in the package, but outside the package. Hence, when I tried run > run configurations, it couldn't find the main method because it was not in the package. All I did was copy the files into the package and Eclipse was then able to detect the main method. So ultimately make sure that Eclipse can find your main method, by making sure that your java files are in the right package.
If so simple than many people think, me included :)
cd to Project Folder/src/package there you should see yourClass.java then run javac yourClass.java which will create yourClass.class then cd out of the src folder and into the build folder there you can run java package.youClass
I am using the Terminal on Mac or you can accomplish the same task using Command Prompt on windows
If you are using Eclipse... I renamed my main class file and got that error. I went to "Run As" configurator and under the class path for that project, it had listed both files in the class path. I removed old class that I renamed and left the class that had the new name and it compiled and ran just fine.
This solved the issue for me today:
cd /path/to/project
cd build
rm -r classes
Then clean&build it and run the individual files you need.
I have a similar problem in Windows, it's related to the classpath. From the command line, navigate until the directory where it's located your Java file (*.java and *.class), then try again with your commands.
I use Anypoint Studio (an Eclipse based IDE). In my case everything worked well, until I found out that while running the java code, something totally different is executed. Then I have deleted the .class files. After this point I got the error message from this question's title. Cleaning the project didn't solve the problem.
After restarting the IDE everything worked well again.