How to use SQLite3 with Java - java

I am trying to build a simple java program which creates a db file, then a table and inserts dummy values in the table. I found this page http://www.zentus.com/sqlitejdbc/index.html and tried out the example given on the page but I am getting the following error -
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Test. Program will exit.

Well that looks like it's a matter of the classpath not being right.
My guess is that you're
If you've put something like
java -cp sqlitejdbc-v056.jar Test
then you probably just need to add the current directory to the classpath:
# Windows
java -cp sqlitejdbc.jar-v056;. Test
# Unix
java -cp sqlitejdbc.jar-v056:. Test
Having looked at that page, my guess is that you used : as the classpath separator, as shown on the page, rather than ; which you need to use if you're on Windows.

Are you sure you're building the test correctly? Here are the steps you'll need to take:
Create a file named Test.java, because it will contain the class named Test
Paste the Java code from the Getting Started section into this file and save it
Download the sqlitejdbc-v056.jar file
and put it in the same directory as the Test.java file
Use javac to compile Test.java into Test.class
Run the command from the Getting Started section
You should see:
name = Gandhi
job = politics
name = Turing
job = computers
name = Wittgenstein
job = smartypants
as output.

Related

NoClassDefFoundError, in the same directory why the program can't find the jar

I write a Java program that read text from excel.So I import some jar like this: poi-3.10-beta2-20130904.jar, poi-ooxml-3.10-beta2-20130904.jar, etc., I can run the program correctly in eclipse.But when I package this program with maven to the directory(C:\workspace2\change\bin),
Then I run this program in command like this :
C:\workspace2\change\bin>java GenerateVar
it occurs this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Cell
at GenerateVar.execute(GenerateVar.java:59)
at GenerateVar.main(GenerateVar.java:25)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Cell
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
You need to specify classpath with all other jars/classes you use in your program.
E.g. Setting multiple jars in java classpath
When you run it from eclipse in the console there's a command that could be used to run the project, you could copy/paste it from the console to your command line.
In the command java GenerateVar you didn't set -cp option. With this option the command line will look like
java -cp poi-3.10-beta2-20130904.jar poi-ooxml-3.10-beta2-20130904.jar ... GenerateVar
Assumed the libraries are in the current dir.
How to use maven plugins to add dependency to your project and build jar you can find here.
See also this answer if you want to modify manifest.mf manually.
over, I change the way do what I want to do. I use the fat jar which is the eclipse plugin to package the program

JAVA: Unable to run a jar file

I'm new to JAVA and trying unable to figure out what's wrong with the project I created.
I created a JAVA project in eclipse and exported a jar (not runnable jar).
I unchecked .project and .classpath file options while exporting. Now I'm trying to run that jar and getting NoClassDefFoundError:
I'm pasting the command and the errors below:
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar -jar D:\Web\HbaseGateway\bin\HBaseGateway.jar
Exception in thread "main" java.lang.NoClassDefFoundError: py4j/GatewayServer
at hbase.gateway.HBaseGatewayEntryPoint.main(HBaseGatewayEntryPoint.java
:22)
Caused by: java.lang.ClassNotFoundException: py4j.GatewayServer
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Now, interesting thing is I'm able to run the main class using the following command.
In the below command I removed the -jar switch and passed the main class to java.exe.
C:\Users\Anjali>"C:\Program Files\Java\jre7\bin\java.exe" -classpath D:\Web\Hbas
eGateway\bin;C:\Users\Anjali\Downloads\Hbase\hbase-0.94.5-security.jar;C:\Users\
Anjali\Downloads\Hbase\lib\hadoop-core-1.0.4.jar;C:\Users\Anjali\Downloads\Hbase
\py4j0.7.jar hbase.gateway.HBaseGatewayEntryPoint
Gateway Server Started
Why NoClassDefFoundError is being thrown? Am I missing something here?
For -jar option to work, you must add Main-class key in manifest.
Refer the java command documentation http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html
-jar option
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name.
In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point.
When you use the -jar option, the classpath is read from the MANIFEST.mf file as well. From the docs for the -jar option:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
This means that you have to add all your dependencies in a Class-Path section of the manifest file.

TestNG command line not working (java org.testng.TestNG )

i'm trying to run testng tests using command line like this :
C:\Documents and Settings\Administrateur\Bureau\automatic tests testNG>java -cp
C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\
testng-6.3.1.jar org.testng.TestNG testng.xml
but i'm getting this error :
Exception in thread "main" java.lang.NoClassDefFoundError: and
Caused by: java.lang.ClassNotFoundException: and
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: and. Program will exit.
So How to add testNG to the classpath.
Thanks
The spaces in the filename are confusing things - Java thinks that only the c:\Documents part is in the classpath argument. Try this:
java -cp
"C:\Documents and Settings"\Administrateur\.m2\repository\org\
testng\testng\6.3.1\testng-6.3.1.jar org.testng.TestNG testng.xml
(where the whole thing is on one line and there's no break after "org" of course)
If you want to run via java on command line, you'll need to either enquote your path ("C:\Documents and Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\") or alter slightly as follows: C:\Documents%20and%20Settings\Administrateur\.m2\repository\org\testng\testng\6.3.1\. You may also want to run via maven to avoid having to specify this classpath info.

cannot run a simple java code

I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:
java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: first. Program will exit. Exception in thread "main"
I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.
A java program has this basic structure:
ClassName.java
public class ClassName
{
public static void main(String[] args)
{
}
}
try using this outline to generate your code.
compile and run with:
javac ClassName.java
java ClassName
I used to get this error when I ran a Class file.
Try doing: java NameOfClass
You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.
Did you set your classpath?
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
java -classpath <path> <classname>
You must provide with the code.
Anyway, from the Java Docs, class ClassNotFoundException:
Thrown when an application tries to
load in a class through its string
name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name
could be found.
Must read link : Tip: Causes of java.lang.ClassNotFoundException
It is quite possible after compiling your code you would be writing java filename.java.
Because this sought of exception occurs then.
After compiling your program using javac filename.java use the command to start your interpreter java filename.
As you enter this command java interpreter automatically starts interpreting filename.class.
commands :
javac filename.java // to start compiling
java filename // to start interpreting

Execute Java Class from VB.Net

I have a class in java "Main.class", wrote and stored in %TEMP%. When executing the class through VB.Net Shell, eg:
Shell("cmd.exe /k java %TEMP%\Main.class")
Also when trying to execute manually through CMD: "java %TEMP%\Main.class", I am returned with:
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\Ben\AppData\
Local\Temp\Main/class
Caused by: java.lang.ClassNotFoundException: C:\Users\Ben\AppData\Local\Temp\Mai
n.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: C:\Users\Ben\AppData\Local\Temp\Main.class. Prog
ram will exit.
However, when I execute Main.class manually through compile.bat - the class runs fine. What is the reasoning for this?
You need to add -classpath to it. Basically the Java interpreter does not know where to find this "Main" class.
CoolBean's solution should work. Something like
Shell("cmd.exe /k java -classpath %TEMP% Main")
Since you can (and are supposed to) omit the .class extension. And like CoolBeans said, you set the directory your class file lives in as the classpath.
While javac takes a file, java takes a Class (in other words, the name of the class after 'public class'), along with what packages it is in (if you don't have "package something;" at the top of your java file, don't worry about this), and it'll look for that class in the classpath you provide, or the current working directory.
If that does end up being the solution, give CoolBeans the accepted answer.
However, an alternative solution is to change the current working directory for Shell to %TEMP%, like:
IO.Directory.SetCurrentDirectory(Environ("TEMP"))
Shell("cmd.exe /k java Main")
Or alternatively look into the Process class, which offers more fine control over launching other programs (and with Process you can also change the directory of the program you're launching without changing the current directory of your own application).
Try this,
Shell("java.exe -cp .;" & Environment.GetEnvironmentVariable("TEMP") & " Main")
OR
Dim args As String = String.Format("-cp .;{0} {1}", Environment.GetEnvironmentVariable("TEMP"), "Main")
Dim procInfo As New ProcessStartInfo
procInfo.FileName = "java.exe"
procInfo.Arguments = args
Dim proc As New Process
proc.StartInfo = procInfo
proc.Start()
proc.WaitForExit()

Categories

Resources