I am using eclipse to try and get a list of sheets from an excel spreadsheet but when I go to run it Java throws exceptions.
here is the code;
File myFile = new File("excel.xlsx");
Workbook wb = null;
try {
wb = WorkbookFactory.create(myFile);
} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<String> sheetNames = new ArrayList<String>();
for (int i=0; i<wb.getNumberOfSheets(); i++) {
sheetNames.add( wb.getSheetName(i) );
}
System.out.println(sheetNames);
here is the log;
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
at com.cogentautomation.view.View.main(View.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
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
I have included the following in the eclipse java build path as libraries;
commons-codec
commons-logging
junit
log4j
poi
poi-ooxml
poi-scratchpad
I know the solution is something simple but my mind is blanking. I am not using maven.
You will need to add XMLBeans dependency.You can include the dependency from here https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans/2.6.0
As the exception message is telling you, you need the XMLBeans library. Poi probably depends on it.
If you were using a dependency management tool such as Gradle or Maven, it would have automatically been included.
Related
public static void main(String[] args) {
try {
PdfViewerStarter jt = new PdfViewerStarter();
jt.doConversion("http://pd4ml.com/sample.htm", "D:/pd4ml.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
public void doConversion(String url, String outputPath)
throws InvalidParameterException, MalformedURLException, IOException {
File output = new File(outputPath);
java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
PD4ML pd4ml = new PD4ML();
pd4ml.setHtmlWidth(userSpaceWidth);
pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
pd4ml.useTTF("c:/windows/fonts", true);
pd4ml.render(new URL(url), fos);
fos.close();
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(output);
} else {
System.out.println("Awt Desktop is not supported!");
}
System.out.println(outputPath + "\ndone.");
}
Error:
Error. ss_css2.jar is not in the classpath. See README.txt
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/css/sac/CSSException
at org.zefer.html.doc.PD4MLHtmlParser.o00000(Unknown Source)
at org.zefer.html.doc.PD4MLHtmlParser.<init>(Unknown Source)
at org.zefer.pd4ml.PD4ML.super(Unknown Source)
at org.zefer.pd4ml.PD4ML.render(Unknown Source)
at TestForPdfPD4ML.doConversion(TestForPdfPD4ML.java:42)
at TestForPdfPD4ML.main(TestForPdfPD4ML.java:24)
Caused by: java.lang.ClassNotFoundException: org.w3c.css.sac.CSSException
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 6 more
It may be asking for the ClassPath of the Jar File where the needed (imported) libraries are contained. If you are running from Command Line try something like this:
java -cp .;"The full path where the Jar File is\jarfile.jar" YourRunningClassFile
I am trying to use Java DB for app only storage.
Environment Variables:
CLASSPATH:
%JAVA_HOME%\jre\lib;%DERBY_HOME%\lib\derby.jar
DERBY_HOME:
C:\Program Files\Java\jdk1.7.0_25\db
Path:
%DERBY_HOME%\bin
No changes in eclipse configurations was done and i cannot include it to build path(it somehow will be considered as third party library, which is not allowed)
When i type "sysinfo" in cmd it tells me Db is instaled and list packages and other info.
Code:
public class Main {
private static String dbURL = "jdbc:derby:AssertDB;user=me;password=mine;create=true";
private static Connection con = null;
public static void main(String[] args) {
setDBSystemDir();
createConnection();
CookiesTable pTable = new CookiesTable(con);
try {
pTable.createTable();
pTable.populateTable();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
// load files
// parse files to tables
// retrive SQL statment
// print result
}
private static void setDBSystemDir() {
// Decide on the db system directory: <userhome>/.addressbook/
String userHomeDir = System.getProperty("user.home", ".");
String systemDir = userHomeDir + "/.asertdb";
// Set the db system directory.
System.setProperty("derby.system.home", systemDir);
}
private static void createConnection() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
// Get a connection
con = DriverManager.getConnection(dbURL);
} catch (Exception except) {
System.out.println("DRIVER DRROOOOOP");
except.printStackTrace();
}
}
}
Exception:
java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.tuto.p4.Main.createConnection(Main.java:44)
at com.tuto.p4.Main.main(Main.java:13)
Exception in thread "main" java.lang.NullPointerException
at com.tuto.p4.CookiesTable.createTable(CookiesTable.java:29)
at com.tuto.p4.Main.main(Main.java:16)
Refering to the documentation there are two things to do in case you get a java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver if you run the SampleApp.
Configure Embedded Derby
Verify Derby
As you said that you checked sysinfo I would try it with adding derby.jar and derbytools.jar even if the tools are optional.
Or you can compare the SampleApp to your App....
Include derbytools.jar, derbyclient.jar as well.
Try putting System.out.println(System.getProperty("java.class.path"));
before Class.forName(...).newInstance()
The output must contain a valid path to derby.jar.
In an android app, having:
public class Pars extends Activity{
public Document docout(){
InputStream is = getResources().openRawResource(R.raw.talechap01);
Document docout = null;
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = domFactory.newDocumentBuilder();
docout = builder.parse(is);
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return docout;
}
and
public class Manager {
public static String getStory(String spage, Document docs) {
XPathExpression expr;
XPath xpath = XPathFactory.newInstance().newXPath();
Object result = null;
String num = spage;
String par = null;
try {
expr = xpath.compile("//decision"+num+"/p//text()");
result = expr.evaluate(docs, XPathConstants.NODESET);
}
catch (XPathExpressionException e) {
e.printStackTrace();
}
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
par = nodes.item(i).getNodeValue();
System.out.println(par);
}
return par;
}
}
Now I want to test the code.
public class Test {
public static void main(String[] args) {
try {Pars p = new Pars();
Document doc = p.docout();
System.out.println(Manager.getStory("000", doc));
}catch (Exception e){
e.printStackTrace();
}
}
}
I run "Test" as a java application and I get:
Exception in thread "main" java.lang.NoClassDefFoundError:
android/app/Activity 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.stack.over.Test.main(Test.java:11) Caused by:
java.lang.ClassNotFoundException: android.app.Activity 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) ... 13 more
I tried to find how to fix it but cannot achieve it.
I have this answer from another question but I'm kind of noob yet and can't figure it out how to fix it.
You have defined your Pars class as extending the class Activity. The
error you are seeing is telling you that the class
android.app.Activity is not available on the classpath. Maybe you need
to check which libraries you import. – adamretter Mar 5 at 10:01
Thanks all.
The error is that you're trying to run a program that references Android classes (android.app.Activity) as a regular Java program that doesn't have Android runtime libraries in its classpath.
You should run Android apps as Android apps (on an emulator or a device) and regular Java programs as regular Java programs and not mix the two. Activity is Android; main() method is regular Java.
To learn Android app development, you can start with the developer.android.com getting started documentation.
The error message says it all. You are writing Android now. Trying to write a main method doesn't make any more sense in Android than it would if you were writing a webapp that runs in Tomcat. You are going to need to step back and read up on how Android works.
I appear to be able to connect fine since new RestS3Service doesn't throw an exception.
However, when I try to do something like getBucket("name"), then I get: Invalid class name: org.jets3t.service.utils.RestUtils$ConnManagerFactory
Any idea how to solve this?
Thanks!
My code:
AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
S3Service s3Service = null;
System.out.println("Credentials created, trying Service");
try {
s3Service = new RestS3Service(awsCredentials);
} catch (S3ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Service connected, trying bucket grab");
try {
S3Bucket bucket = s3Service.getBucket("mrbucket");
System.out.println("bucket grabbed: "+bucket.getName());
} catch (S3ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Output:
Credentials created, trying Service
Installer - Installer service created
Service connected, trying bucket grab
org.jets3t.service.S3ServiceException: Request Error: java.lang.IllegalStateException: Invalid class name: org.jets3t.service.utils.RestUtils$ConnManagerFactory
at org.jets3t.service.S3Service.getBucket(S3Service.java:1911)
at org.red5.core.Application.myFunction(Application.java:78)
at org.red5.core.Application.appStart(Application.java:42)
at org.red5.server.adapter.MultiThreadedApplicationAdapter.start(MultiThreadedApplicationAdapter.java:408)
at org.red5.server.adapter.ApplicationAdapter.start(ApplicationAdapter.java:54)
at org.red5.server.Scope.start(Scope.java:1170)
at org.red5.server.Scope.init(Scope.java:905)
at org.red5.server.WebScope.register(WebScope.java:207)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1544)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.red5.server.tomcat.TomcatLoader$1.run(TomcatLoader.java:577)
Caused by: java.lang.IllegalStateException: Invalid class name: org.jets3t.service.utils.RestUtils$ConnManagerFactory
at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:308)
at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:445)
at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:274)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:797)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
at org.jets3t.service.impl.rest.httpclient.RestStorageService.performRequest(RestStorageService.java:334)
at org.jets3t.service.impl.rest.httpclient.RestStorageService.performRequest(RestStorageService.java:281)
at org.jets3t.service.impl.rest.httpclient.RestStorageService.performRestGet(RestStorageService.java:981)
at org.jets3t.service.impl.rest.httpclient.RestStorageService.listAllBucketsImpl(RestStorageService.java:1373)
at org.jets3t.service.StorageService.listAllBucketsImpl(StorageService.java:1379)
at org.jets3t.service.StorageService.listAllBuckets(StorageService.java:563)
at org.jets3t.service.S3Service.listAllBuckets(S3Service.java:1367)
at org.jets3t.service.S3Service.listAllBuckets(S3Service.java:84)
at org.jets3t.service.StorageService.getBucket(StorageService.java:729)
at org.jets3t.service.S3Service.getBucket(S3Service.java:1909)
... 24 more
It looks like org.jets3t.service.utils.RestUtils$ConnManagerFactory.class is missing from your class path. Do you have the entire JetS3t library available? There are about 19 jars in the library, RestUtils$ConnManagerFactory.class is in jet3t-0.8.1a.jar in the 0.8.1a version.
Groovy users: this exception is also thrown if you are declaring jets3t as a dependency in a groovy script with Grape.
If this is your case, you’ll need to configure Grape to use the system class loader:
#GrabConfig(systemClassLoader=true)
#Grab(group='net.java.dev.jets3t', module='jets3t', version='0.9.4')
I'm trying to execute this code from the HTMLUnit tutorial:
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
but I get the MalformedURLException in the second line when saving it in Eclipse (if I compile and run the code, I get it too). What's the problem? TIA
PS: I'm new to Java
Up:
Here's the stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/httpclient/auth/CredentialsProvider
at Tester.main(Tester.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider
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.loadClassInternal(Unknown Source)
... 1 more
I tried this in Eclipse 3.5 and it is working correctly, and the test is passing. I assume that you have included the necessary HTMLUnit JARs into your project? I took all the JARs from the HTMLUnit lib directory and added them to the build path of my project.
Also, can you catch the exception and post the stack trace here?
try {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
}
catch (Exception e) {
e.printStackTrace();
}
package com.project.test;
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Practice1 {
public static void main(String[] args) {
final WebClient webClient = new WebClient();
HtmlPage page;
try {
page = (HtmlPage) webClient.getPage("http://htmlunit.sourceforge.net");
System.out.println("Title="+ page.getTitleText());
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Since in your stacktrace you have
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.auth.CredentialsProvider
this isn't a malformed URL exception.
As confirmed by JARFinder, org.apache.commons.httpclient.auth.CredentialsProvider class should come from commons-httpclient-3.*.jar. Thus the cause of the problem must be that you don't have Commons HTTPClient 3.x JARs in your classpath.