I have read the questions on using Sqoop from within a Java program here, here and here.
I came up with the following, but I am stumped by a ClassNotFoundException:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import com.cloudera.sqoop.SqoopOptions;
import com.cloudera.sqoop.SqoopOptions.FileLayout;
import com.mysql.jdbc.*;
public class SqoopExample {
public static void main(String[] args) throws Exception {
String driver = "com.mysql.jdbc.driver";
Class.forName(driver);
Configuration config = new Configuration();
// note that this is HDFS path, rather than core path
config.addResource(new Path("./config/core-site.xml"));
config.addResource(new Path("./config/hdfs-site.xml"));
FileSystem dfs = FileSystem.get(config);
SqoopOptions options = new SqoopOptions();
options.setDriverClassName(driver);
options.setConnectString("jdbc:mysql://localhost:3306/dbname");
options.setTableName("v_webstats");
options.setUsername("root");
options.setNumMappers(1);
options.setTargetDir(dfs.getWorkingDirectory()+"/TestDirectory");
options.setFileLayout(FileLayout.TextFile);
new com.cloudera.sqoop.tool.ImportTool().run(options);
}
}
I am sure that all the jars are correctly included and I have tested the connection to my database server and that works as well.
The exact error is:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at SqoopExample.main(SqoopExample.java:14)
not sure what I am doing wrong.
I believe that proper name of the MySQL JDBC driver class is:
com.mysql.jdbc.Driver
(notice the capital "D" in the "Driver)
Related
I'm using APSTEX IFC Framework to creat a IFC 3D Viewer in my Java program.
My code is as follow.
import com.apstex.ifcjava3dviewer.IfcJava3DViewer;
public class JavaViewer2 {
public static void main(String[] args) {
IfcJava3DViewer ifcViewer = new IfcJava3DViewer();
}
}
The error is as followed:
java.lang.NoClassDefFoundError: com/jogamp/opengl/GLCapabilitiesImmutable
at com.apstex.javax.media.j3d.Pipeline$PipelineCreator.run(SourceFile:74)
at com.apstex.javax.media.j3d.Pipeline$PipelineCreator.run(SourceFile:1)
at java.security.AccessController.doPrivileged(Native Method)
at com.apstex.javax.media.j3d.Pipeline.b(SourceFile:91)
at com.apstex.javax.media.j3d.MasterControl.c(SourceFile:858)
at com.apstex.javax.media.j3d.VirtualUniverse.<clinit>(SourceFile:267)
at com.apstex.gui.core.controller.ApplicationController.<init>(SourceFile:1097)
at com.apstex.gui.core.kernel.Kernel.getApplicationController(SourceFile:59)
at com.apstex.ifcjava3dviewer.IfcJava3DPanel.<init>(SourceFile:71)
at com.apstex.ifcjava3dviewer.IfcJava3DViewer.<init>(SourceFile:38)
at test.main.JavaViewer2.main(JavaViewer2.java:7)
Caused by: java.lang.ClassNotFoundException: com.jogamp.opengl.GLCapabilitiesImmutable
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)
... 11 more
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class com.apstex.javax.media.j3d.VirtualUniverse
at com.apstex.javax.media.j3d.Canvas3D.<clinit>(SourceFile:3821)
at com.apstex.gui.ifc.views.view3d.j3d.ModelViewer.<init>(SourceFile:93)
at com.apstex.gui.ifc.views.view3d.j3d.ModelViewer.<init>(SourceFile:79)
at com.apstex.ifcjava3dviewer.IfcJava3DPanel.<init>(SourceFile:1079)
at com.apstex.ifcjava3dviewer.IfcJava3DViewer.<init>(SourceFile:38)
at test.main.JavaViewer2.main(JavaViewer2.java:7)
How can I correct the error?
Thanks for the two commends above to help me solve the problem. By installing a library of JOGL the program works.
I have a jar generated with jdk 1.6, it's working fine with jdk 1.7 and 1.7 but I get the following exception when trying to run it with 1.9 :
Exception in thread "main" java.lang.SecurityException: Prohibited package name: java.sql
at java.base/java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
at java.base/java.security.SecureClassLoader.defineClass(Unknown Source)
at java.base/java.net.URLClassLoader.defineClass(Unknown Source)
at java.base/java.net.URLClassLoader.access$100(Unknown Source)
at java.base/java.net.URLClassLoader$1.run(Unknown Source)
at java.base/java.net.URLClassLoader$1.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.net.URLClassLoader.findClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.base/java.lang.Class.getMethodsRecursive(Unknown Source)
at java.base/java.lang.Class.getMethod0(Unknown Source)
at java.base/java.lang.Class.getMethod(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:57)
my main class :
package eai_principale;
import com.siebel.data.SiebelException;
import java.io.IOException;
import java.sql.SQLException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class MainExecute
{
public static void main(String[] args)
throws SQLException, SiebelException, ParserConfigurationException, SAXException, IOException
{
System.out.println("-----------------START EAI CONNECTOR EXECUTION------------------");
Principale execute = new Principale();
String ligneId = args[0];
String path = args[1];
execute.execute(ligneId, path);
System.out.println("-----------------END EAI CONNECTOR EXECUTION------------------");
}
}
Could you help me ?
I am using Eclipse, and I am using jdbc lib .
Thx
Looks like a bug in Eclipse JDT, specifically in the jarinjarloader part. It is referenced as Bug 525885 - jarinjarloader with jdbc jar does not work in jre9 in Eclipse Bugzilla.
The bugzilla article suggests that using a standard MANIFEST could be a workaround:
The thing is that my jar will execute in jre9 if I manually remove the jarinjarloader portion and use a standard MANIFEST file.
I'm trying to write a simple Jena Fuseki client in Java to send a SELECT SPARQL query and get the result.
I've found the following code written by someone else. It keeps crashing at runtime.
I'm using jena-arq-3.1.0.jar
javac -cp .;jena-arq-3.1.0.jar Main.java print no errors
java -cp .;jena-arq-3.1.0.jar Main crash!
Error log
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/jena/atlas/io/Printable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:78)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:52)
at org.apache.jena.query.QueryFactory.create(QueryFactory.java:40)
at Main.main(Main.java:24)
Caused by: java.lang.ClassNotFoundException: org.apache.jena.atlas.io.Printable
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)
... 16 more
Main.java
import java.net.URL;
import java.util.List;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
class Main {
public static void main(final String[] args) throws Exception {
String queryString=
"prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"prefix owl: <http://www.w3.org/2002/07/owl#>"+
"SELECT ?subject ?predicate ?object"+
"WHERE {"+
" ?subject ?predicate ?object"+
" } LIMIT 25";
// now creating query object
Query query = QueryFactory.create(queryString);
// initializing queryExecution factory with remote service.
// **this actually was the main problem I couldn't figure out.**
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://localhost:3030/FirstEndpoint/sparql", query);
//after it goes standard query execution and result processing which can
// be found in almost any Jena/SPARQL tutorial.
try {
ResultSet results = qexec.execSelect();
for (; results.hasNext();) {
// Result processing is done here.
}
}
finally {
qexec.close();
}
}
}
You need all necessary libs in the classpath, not only jena-arq.
javac works because you're using only classes from the jena-arq JAR.
java doesn't work because at runtime the classes of ARQ refer to other classes contain e.g. in jena-core, jena-iri, etc.
The easiest way would to add all libs from the Jena distribution resp. the lib folder itself to the classpath.
I am trying to write a little test-programm with different Databases. MongoDB is one of them and it worked perfectly fine until last Friday, even though I didn't change anything in the java code of my MongoDB class.
System: I use an ubuntu 14.04 VM with the package mongo-org. The version of mongo is 3.0.5.
I also run MariaDB, PostgreSQL and Cassandra on the VM.
As a Client, I have windows 7 and Eclipse.
The connection and the test work perfectly fine with the other 3 databases.
And I can run the "mongo" client on ubuntu as well.
But when I try out the Java Test of MongoDB, I keep getting this error:
Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at com.mongodb.MongoClientOptions$Builder.<init>(MongoClientOptions.java:52)
at com.mongodb.MongoClient.<init>(MongoClient.java:126)
at com.mongodb.MongoClient.<init>(MongoClient.java:115)
at de.motza.database.impl.DatabaseImplMongoDB.connect(DatabaseImplMongoDB.java:197)
at de.motza.database.impl.DatabaseImplMongoDB.instantiate(DatabaseImplMongoDB.java:207)
at de.motza.test.History_Log.testNewDatabase(History_Log.java:86)
at de.motza.test.History_Log.main(History_Log.java:66)
The part of the java Class, where the Exception occurs:
private static String dbHost = "10.158.251.251";
private static int dbPort = 27017;
private static String dbName = "history_log";
private static MongoClient mongoClient = null;
[...]
#Override
public void connect() {
try {
mongoClient = new MongoClient(dbHost, dbPort);
db = mongoClient.getDB(dbName);
} catch (Exception e) {
log.error(e);
}
}
I read about the disk-space, but I habe 12 GB free in my VM, so I do not think that this is the problem. I ran apt-get upgrade, but it didn't change anything either. I also reinstalled mongoDb, but nothing changed.
I also tried to recompile, but nothing changed there either.
Does anybody have any advice?
ADDITION:
Here you find the library tab of my project. I use multiple databases, so I marked the ones important for MongoDB
One possible reason for a IncompatibleClassChangeError you get is that a Mongo library binary has changed (for whatever reason) and now your earlier compiled test code isn't any more compatible with the library. I would suggest to recompile your test code and try again.
I am having problems loading the MySQL JDBC driver. I have tried everything mentioned here, but am still running into problems.
The error I get is this:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Main.main(Main.java:12)
My code is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Main {
public static void main(String args[]) throws Exception {
//accessing the driver
Class.forName("com.mysql.jdbc.driver");
// creating variable to pass the connection information into
Connection dbcon = DriverManager.getConnection(
"jdbc:mysql//localhost:3306/test", "root", "root");
PreparedStatement statement = dbcon
.prepareStatement("select * from names");
ResultSet result = statement.executeQuery();
while (result.next()) {
System.out.println(result.getString(2));
}
}
}
As you can see above I have placed the driver in the lib folder and I have also put it into the Apache tomcat lib folder, because that was suggested as well.
Thank you for any help you can give me.
Andrew
The class name inside the MySql connectorJ JAR file is Driver. so you need to change
Class.forName("com.mysql.jdbc.driver");
to
Class.forName("com.mysql.jdbc.Driver");
It tells you the problem on the first line of your stacktrace:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
The driver name is com.mysql.jdbc.Driver (capital D).
problem in loading the mysql connection library.please check the proper mysql library is load successfully.because when library load successfully then the library package display all the external loaded library.