I'm tring to dispaly data from an MS Access db, I'm useing Jackcess library but I get a dependence error that I do not how to resolve. Here is my code and error:
import com.healthmarketscience.jackcess.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.nio.*;
import java.lang.*;
import org.apache.commons.*;
import org.apache.commons.lang3.builder.*;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
class Main {
private static Logger log=Logger.getLogger(Main.class);
public static void main(String args[]) {
DOMConfigurator.configure("lib\\log4j.xml");
try {
Database d = Database.open(new File("lib\\ExTables.mdb"));
System.out.println(d.getTable("Cliente").display());
} catch(Exception e) {
e.printStackTrace();
}
}
}
Here is my error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/builder/CompareToBuilder
at com.healthmarketscience.jackcess.RowId.compareTo(RowId.java:108)
at com.healthmarketscience.jackcess.IndexData$Entry.compareTo(IndexData.java:1825)
at com.healthmarketscience.jackcess.IndexData$Entry.compareTo(IndexData.java:1637)
at java.util.Collections.indexedBinarySearch(Unknown Source)
at java.util.Collections.binarySearch(Unknown Source)
at com.healthmarketscience.jackcess.IndexData$DataPage.findEntry(IndexData.java:2347)
at com.healthmarketscience.jackcess.IndexData.findEntryPosition(IndexData.java:709)
at com.healthmarketscience.jackcess.IndexData.access$3100(IndexData.java:54)
at com.healthmarketscience.jackcess.IndexData$EntryCursor.updatePosition(IndexData.java:2118)
at com.healthmarketscience.jackcess.IndexData$EntryCursor.restorePosition(IndexData.java:2057)
at com.healthmarketscience.jackcess.IndexData$EntryCursor.restorePosition(IndexData.java:2040)
at com.healthmarketscience.jackcess.IndexData$EntryCursor.beforeEntry(IndexData.java:2002)
at com.healthmarketscience.jackcess.IndexCursor.findPotentialRow(IndexCursor.java:439)
at com.healthmarketscience.jackcess.IndexCursor.findRowByEntryImpl(IndexCursor.java:342)
at com.healthmarketscience.jackcess.IndexCursor.findRowByEntry(IndexCursor.java:175)
at com.healthmarketscience.jackcess.Database$DefaultTableFinder.findRow(Database.java:2239)
at com.healthmarketscience.jackcess.Database$TableFinder.findObjectId(Database.java:2168)
at com.healthmarketscience.jackcess.Database.readSystemCatalog(Database.java:1081)
at com.healthmarketscience.jackcess.Database.<init>(Database.java:765)
at com.healthmarketscience.jackcess.Database.open(Database.java:589)
at com.healthmarketscience.jackcess.Database.open(Database.java:535)
at com.healthmarketscience.jackcess.Database.open(Database.java:510)
at com.healthmarketscience.jackcess.Database.open(Database.java:488)
at com.healthmarketscience.jackcess.Database.open(Database.java:467)
at Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.builder.CompareToBuilder
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)
... 25 more
That's my command line:
C:\Programmi\Java\jre6\bin\javaw.exe -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:3280 -Dfile.encoding=Cp1252 -classpath "C:\Documents and Settings\linosa\Documenti\programmazione\workspace_java\MSAccess\bin;C:\Documents and Settings\linosa\Documenti\programmazione\workspace_java\MSAccess\lib\jackcess-1.2.5.jar;C:\Documents and Settings\linosa\Documenti\programmazione\workspace_java\MSAccess\lib\log4j-1.2.16.jar;C:\Documents and Settings\linosa\Documenti\programmazione\workspace_java\MSAccess\lib\commons-logging-1.1.1.jar;C:\Documents and Settings\linosa\Documenti\programmazione\workspace_java\MSAccess\lib\junit-4.10.jar;C:\Documents and Settings\linosa\Documenti\programmazione\commons-lang3-3.1-bin\commons-lang3-3.1\commons-lang3-3.1-tests.jar;C:\Documents and Settings\linosa\Documenti\programmazione\commons-lang3-3.1-bin\commons-lang3-3.1\commons-lang3-3.1.jar;C:\Documents and Settings\linosa\Documenti\programmazione\commons-lang3-3.1-bin\commons-lang3-3.1\commons-lang3-3.1-javadoc.jar;C:\Documents and Settings\linosa\Documenti\programmazione\commons-lang3-3.1-bin\commons-lang3-3.1\commons-lang3-3.1-sources.jar" Main
Commons Lang 3 is not backward compatible with 2.x. You'll have to download 2.6.
You might also want to investigate Maven or Ant+Ivy to make this kind of dependency resolution a bit easier.
Found it ;-) The commons lang is in the classpath, but it seems to be the wrong version.
The requested package is org.apache.commons.lang.builder, which is valid for commons-lang 2.x in commons-lang3 the package is 'org.apache.commons.lang3.builder'. So you have to include a commons-lang 2.x
Related
I am trying to connect Ms Access database with java 8 version. But as in this version jdbcodbcbridge driver has been removed, so following jar files need to be included :
**ucanaccess-x.x.x.jar
HSQLDB (hsqldb.jar, version 2.2.5 or newer)
,Jackcess (jackcess-2.x.x.jar)
,commons-lang (commons-lang-2.6.jar, or newer 2.x version)
,commons-logging (commons-logging-1.1.1.jar, or newer 1.x version)**
I have bought all these jar files in my eclipse through Build Path option.
But still when i am executing the following code it is coming up with error as:
Exception in thread "main" java.lang.NoClassDefFoundError:
com/healthmarketscience/jackcess/util/ErrorHandler at java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Unknown Source) at
demo.JDBCDemo.main(JDBCDemo.java:11) Caused by:
java.lang.ClassNotFoundException:
com.healthmarketscience.jackcess.util.ErrorHandler at
java.net.URLClassLoader.findClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) at
java.lang.ClassLoader.loadClass(Unknown Source) ... 3 more
And my code is:
import java.sql.*;
import java.util.*;
import com.healthmarketscience.jackcess.util.ErrorHandler;
public class JDBCDemo {
public static void main(String args[]) throws Exception
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection con=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\isha\\Desktop\\StudentData.accdb");
Statement stmt=con.createStatement();
String str="insert into NameData values(4,'ram')";
stmt.executeUpdate(str);
String s="select * from NameData";
ResultSet res=stmt.executeQuery(s);
while(res.next()){
System.out.println(res.getString(1)+":"+res.getString(2));
Enumeration e=DriverManager.getDrivers();
while(e.hasMoreElements()){
Driver d=(Driver)e.nextElement();
System.out.println(d.getClass().getName());
}
}
}
}
You have an old obsolete version of jackcess in your classpath. Please add to your classpath ucanaccess.jar the jars in the folder lib of the specific ucanaccess distribution you're using.
String str="insert into NameData values(4,'ram')";
in which columns you insert this values?
I wanted to convert docx to html. I started writing the code same as examples given in github. This is just loading part. There itself I'm getting the problem.
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class Main {
public static void main(String[] args) throws Docx4JException,
String inputfilepath = "myfilepathhere";
OutputStream os = new FileOutputStream(inputfilepath + ".html");
WordprocessingMLPackage wordMLPackage = Docx4J
.load(new FileInputStream(inputfilepath));
}
}
I'm getting NullPointerException. Seeing the exception trace and navigating in source code in github, I suspect it has something to do with JAXB related thing from this class https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/jaxb/Context.java
Docx4j source code is available at https://github.com/plutext/docx4j.
Exception trace:
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: Couldn't get [Content_Types].xml from ZipFile
at org.docx4j.openpackaging.io3.Load3.get(Load3.java:134)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:454)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:371)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:337)
at org.docx4j.openpackaging.packages.OpcPackage.load(OpcPackage.java:302)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.load(WordprocessingMLPackage.java:170)
at org.docx4j.Docx4J.load(Docx4J.java:195)
at Main.main(Main.java:29)
Caused by: org.docx4j.openpackaging.exceptions.InvalidFormatException: Bad [Content_Types].xml
at org.docx4j.openpackaging.contenttype.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:713)
at org.docx4j.openpackaging.io3.Load3.get(Load3.java:132)
... 7 more
Caused by: java.lang.NullPointerException
at org.docx4j.openpackaging.contenttype.ContentTypeManager.parseContentTypesFile(ContentTypeManager.java:679)
... 8 more
The docx document is good (created by Word 2010). I've even unzipped it to see if the Content_Types.xml is there. It's there.
I'm using Eclipse and Java SE 7. I've added all the required jar files to Java build path in project properties.
Please help me.
Update:
Actually when I added this line from Context.java into my class to see if that's the problem.
JAXBContext.newInstance("org.docx4j.openpackaging.contenttype");
I could see the following exception in my console:
Exception in thread "main" javax.xml.bind.JAXBException: Provider org.eclipse.persistence.jaxb.JAXBContextFactory not found
- with linked exception:
[java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory]
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at Main.main(Main.java:26)
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jaxb.JAXBContextFactory
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 javax.xml.bind.ContextFinder.safeLoadClass(Unknown Source)
... 6 more
docx4j supports several different JAXB implementations:
the reference implementation
the one Sun/Oracle include in Java 6/7/8
EclipseLink MOXy
If you want to use MOXy, you need:
the relevant EclipseLink jars
docx4j-MOXy-JAXBContext-3.0.0.jar (which just contains the jaxb.properties files)
The jaxb.properties files just say:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
If you are using maven, you'll just need to add:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-MOXy-JAXBContext</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.1</version>
</dependency>
Is the docx4j-MOXy-JAXBContext jar on your classpath? Either remove it, or add the relevant EclipseLink jars
This works for me, try this out
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.docx4j.Docx4J;
import org.docx4j.Docx4jProperties;
import org.docx4j.convert.out.HTMLSettings;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class Test {
public static void main(String[] args) throws Docx4JException,
FileNotFoundException {
String inputfilepath = "c:/file.docx";
WordprocessingMLPackage wordMLPackage = Docx4J
.load(new FileInputStream(inputfilepath));
// HTML exporter setup (required)
//.. the HTMLSettings object
HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
htmlSettings.setImageDirPath(inputfilepath + "_files");
htmlSettings.setImageTargetUri(inputfilepath.substring(inputfilepath
.lastIndexOf("/") + 1) + "_files");
htmlSettings.setWmlPackage(wordMLPackage);
OutputStream os = new FileOutputStream(inputfilepath + ".html");
// If you want XHTML output
Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
//Prefer the exporter, that uses a xsl transformation
Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
}
}
Ensure you've all the right dependencies (including the appropriate JAXB Runtime)
implementation 'org.docx4j:docx4j-core:11.4.7'
implementation 'org.docx4j:docx4j-MOXy-JAXBContext:6.0.0'
implementation 'org.docx4j:docx4j-export-fo:11.4.7'
implementation 'org.docx4j:docx4j-JAXB-Internal:8.3.8'
implementation 'org.docx4j:docx4j-JAXB-ReferenceImpl:11.4.7'
implementation 'org.docx4j:docx4j-JAXB-MOXy:11.4.7'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.0'
implementation 'jakarta.activation:jakarta.activation-api:2.1.0'
I have a problem with reading excel file and analyze it through apache poi 3.9... I added the external JAR file but it still gives me errors. Here my codes
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class loop {
public static void main(String [] args) throws Exception
{
File excel= new File("C:\\Users\\songSent.xlsx");
FileInputStream fis= new FileInputStream(excel);
XSSFWorkbook wb= new XSSFWorkbook(fis);
XSSFSheet ws= wb.getSheet("Input");
int rowNum=ws.getLastRowNum() +1;
int colNum=ws.getRow(0).getLastCellNum();
String [][] data= new String[rowNum][colNum];
for(int i=0; i<rowNum; i++)
{
XSSFRow row= ws.getRow(i);
for(int j=0; j<colNum; j++)
{
XSSFCell cell=row.getCell(j);
String value=cellToString(cell);
data[i][j]=value;
System.out.println("the value is " +value);
}
}
}
public static String cellToString(XSSFCell cell)
{
int type;
Object result;
type=cell.getCellType();
switch (type){
case 0:
result=cell.getNumericCellValue();
break;
case 1:
result=cell.getStringCellValue();
break;
default:
throw new RuntimeException("There no support");
}
return result.toString();
}
}
and these are the errors when I run the program
Exception in thread "main" java.lang.NoClassDefFoundError:org/apache/poi/hssf/usermodel/HSSFCell
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException:org.apache.poi.hssf.usermodel.HSSFCell
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)
... 6 more
It is pretty clear from the stack trace that your program is not able to find "poi" jar. Check if your classpath is set correctly & if you are running from eclipse (or some other IDE) check if jar is added to the build path.
At runtime, you're missing at least some of the Apache POI jars. Specifically, you're missing the main poi jar (eg poi-3.10-beta2-20130904.jar), and quite possibly some others too.
Firstly, you need to remember that with Java, your jars need to be available both at compile time and at runtime. Just having the jar on your classpath when you compile isn't enough, you also still need to put them on the classpath at runtime!
Secondly, you'll need to ensure that you include the Apache POI jars as appropriate for the POI components you're using, along with any dependencies those components have. That's all documented on the Apache POI components page, which explains which Jars hold which components, and what dependencies those have.
(Since you're using XSSF, that means you need the main poi jar, the poi-ooxml jar, the poi-ooxml-schemas jar, and their dependencies)
I am using the Apertium Translator and using the sample code they provide. My code is like this.
import com.robtheis.aptr.language.Language;
import com.robtheis.aptr.translate.Translate;
public class Test {
public static void main(String[] args) throws Exception {
// Set the Apertium API Key - Get yours at http://api.apertium.org/register.jsp
Translate.setKey("BNSCFhEL8DoTApc2I1+aa3UYkVg");
String translatedText = Translate.execute("Hola, mundo!", Language.SPANISH, Language.ENGLISH);
System.out.println(translatedText);
}
}
I have no errors or warnings and when I run the program I get the following errors.
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONValue
at com.robtheis.aptr.ApertiumTranslatorAPI.jsonSubObjToString(ApertiumTranslatorAPI.java:195)
at com.robtheis.aptr.ApertiumTranslatorAPI.retrieveSubObjString(ApertiumTranslatorAPI.java:140)
at com.robtheis.aptr.translate.Translate.execute(Translate.java:56)
at maple.Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONValue
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)
... 4 more
The .jar I'm using is the second one from https://github.com/rmtheis/apertium-translator-java-api/downloads
You have to download the first one. The first jar file (apertium-translator-java-api-0.2-jar-with-dependencies.jar) contains the all dependencies needed..
Or you add a json library to your project path..
Well JVM is not able to find some class at Runtime which was available at compile time. That is the reason of NoClassDefFoundError
Below you also have ClassNotFoundException for this class org.json.simple.JSONValue
which means you are trying to load the particular class at run-time by name.
Both of them related to Java Class Path.
I don't know alot about jSON stuff. But you are missing this file org.json.simple.JSONValue you can take it from here http://code.google.com/p/json-simple/
add the above jar file in your class path . And the code will definitely run. Guaranteed.!!!
Thanks
My program below is called Apple.java
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
int y=20,z=0;
z=y+10;
System.out.println("x+y is "+z);
}
}
This is another program called Fruit.java
package food;
public abstract class Fruit{
public static void main(String[] args){
int x=10;
System.out.println("x is "+x);
}
}
Both these programs are stored in the following directory path :- G:\Java\food
I did successfully compile both the programs using the following command:-
G:\>javac Java\food\*.java
and it produced class files.
I did manage to execute the Fruit program using the following command:-
G:\>java -classpath G:\Java food.Fruit
However, when I tried to do the same for the Apple program it resulted in error messages.
G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)
Could someone please help me in figuring out what the error is? I am trying to get myself ready for the SCJP cert exam and hence I reference this example back to the McGrawHill publication book written by Kathy Sierra and Bert Bates.
Edit #2
Yes, I did because that was how the code was in the book. Is there a way I could still execute the file without having to include the package statement.
This is what the question looks like:
Attempt to compile the two files. If you want to use the Apple class, make
sure you place the Fruit.class file in the food subdirectory.
Edit #3
I did try that and got the following errors
G:\>java -classpath G:\Java\food Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Fruit
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
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$000(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)
Caused by: java.lang.ClassNotFoundException: food.Fruit
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)
... 12 more
Could not find the main class: Apple. Program will exit.
Edit #4
I did manage to execute it without having to include the statement
package food;
G:\>java -classpath G:\Java\food;G:\Java Apple
x+y is 30
What I changed was I moved the Apple.class file into a separate folder which makes sense because the folder "food" is an indication of the package and as Darvis pointed all files in the folder needed to have the package name else it would not execute.
Edit #5
This worked too
G:\>java -classpath G:\Java Apple
x+y is 30
because you haven't specified food package for that. make it like this and it will work
package food;
import food.Fruit;
class Apple extends Fruit{
public static void main(String[] args){
int y=20,z=0;
z=y+10;
System.out.println("x+y is "+z);
}
}
Update:
When you execute below
G:\>java -classpath G:\Java food.Apple
Exception in thread "main" java.lang.NoClassDefFoundError: food/Apple (wrong name: Apple)
it will add G:\Java in class path from there it will relatively search for class apple in food DIR with package name specified food
Or. if you don't want to specify package in the class then
G:\>java -classpath G:\Java\food Apple