Documents4j IllegalStateException - java

im trying to use documents4j (https://bintray.com/raphw/maven/documents4j) in order to convert a docx file to pdf with this code:
Thank you very much in advance, i have no idea of making it works
package documents4j;
import java.io.File;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
public class HelloPDF {
public static void main(String[] args) {
File wordFile = new File("c:/datos/Resultados.docx"), target = new File("c:/datos/Resultados.pdf");
IConverter converter = LocalConverter.builder().baseFolder(new File("c:/datos/"))
.workerPool(20, 25, 2, TimeUnit.SECONDS)
.processTimeout(5, TimeUnit.SECONDS).build();
Future<Boolean> conversion = converter.convert(wordFile).as(DocumentType.DOCX).to(target).as(DocumentType.PDF)
// .prioritizeWith(1000) // optional
.schedule();
}
}
But im encountering an illegalstateexcepcion
Exception in thread "main" java.lang.IllegalStateException: class com.documents4j.conversion.msoffice.MicrosoftWordBridge could not be created by a (File, long, TimeUnit) constructor
at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:32)
at com.documents4j.conversion.ExternalConverterDiscovery.makeAll(ExternalConverterDiscovery.java:42)
at com.documents4j.conversion.ExternalConverterDiscovery.loadConfiguration(ExternalConverterDiscovery.java:85)
at com.documents4j.conversion.DefaultConversionManager.<init>(DefaultConversionManager.java:22)
at com.documents4j.job.LocalConverter.makeConversionManager(LocalConverter.java:79)
at com.documents4j.job.LocalConverter.<init>(LocalConverter.java:51)
at com.documents4j.job.LocalConverter$Builder.build(LocalConverter.java:186)
at documents4j.HelloPDF.main(HelloPDF.java:20)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.documents4j.conversion.ExternalConverterDiscovery.make(ExternalConverterDiscovery.java:30)
... 7 more
Caused by: java.lang.NoClassDefFoundError: com/documents4j/util/Reaction$IExceptionBuilder
at com.documents4j.conversion.msoffice.AbstractMicrosoftOfficeBridge.tryStart(AbstractMicrosoftOfficeBridge.java:34)
at com.documents4j.conversion.msoffice.MicrosoftWordBridge.startUp(MicrosoftWordBridge.java:44)
at com.documents4j.conversion.msoffice.MicrosoftWordBridge.<init>(MicrosoftWordBridge.java:39)
... 12 more
Caused by: java.lang.ClassNotFoundException: com.documents4j.util.Reaction$IExceptionBuilder
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 15 more
[Shutdown hook: com.documents4j.job.LocalConverter] INFO com.documents4j.job.ConverterAdapter - Tried to deregister shut down hook in shut down period
java.lang.IllegalStateException: Shutdown in progress
at java.lang.ApplicationShutdownHooks.remove(ApplicationShutdownHooks.java:82)
at java.lang.Runtime.removeShutdownHook(Runtime.java:237)
at com.documents4j.job.ConverterAdapter.deregisterShutdownHook(ConverterAdapter.java:121)
at com.documents4j.job.ConverterAdapter.cleanUp(ConverterAdapter.java:107)
at com.documents4j.job.ConverterAdapter.shutDown(ConverterAdapter.java:98)
at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:109)
at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)
Exception in thread "Shutdown hook: com.documents4j.job.LocalConverter" java.lang.NullPointerException
at com.documents4j.job.LocalConverter.shutDown(LocalConverter.java:100)
at com.documents4j.job.ConverterAdapter$ConverterShutdownHook.run(ConverterAdapter.java:134)

Ok, i have found the answer. The problem was that i have not all the libraries included, i add:
documents4j-util-all-1.0.3
documents4j-util-standalone-1.0.3
And it finally works

Related

Libaries correctly extracted?

A few days ago I switched from the Eclipse IDE to IntelliJ IDE. This is my first time I am coding with IntelliJ. I don't know how I can describe my Problem. :D Where is the Problem? I use this Java Application as a Plugin in my Bungeecord-Proxy
Stacktrace:
20:46:16 [WARNING] Exception encountered when loading plugin:
CloudNet-RestAPI java.lang.ExceptionInInitializerError at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
com.sun.proxy.$Proxy9.(Unknown Source) at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739) at
com.sun.jersey.server.impl.application.WebApplicationImpl$26.run(WebApplicationImpl.java:1626)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.jersey.server.impl.application.WebApplicationImpl.createProxy(WebApplicationImpl.java:1623)
at
com.sun.jersey.server.impl.application.WebApplicationImpl.(WebApplicationImpl.java:335)
at
com.sun.jersey.server.impl.container.WebApplicationProviderImpl.createWebApplication(WebApplicationProviderImpl.java:55)
at
com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:66)
at
com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:160)
at
com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264)
at
com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246)
at
com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117)
at
com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92)
at me.singulativ.restapi.main.RestAPI.onEnable(RestAPI.java:17) at
net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:227)
at net.md_5.bungee.BungeeCord.start(BungeeCord.java:266) at
net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:56)
at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15) Caused by:
java.lang.RuntimeException: java.lang.ClassNotFoundException:
com.sun.ws.rs.ext.RuntimeDelegateImpl at
javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:122)
at
javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:91)
at javax.ws.rs.core.EntityTag.(EntityTag.java:35) ... 24
more Caused by: java.lang.ClassNotFoundException:
com.sun.ws.rs.ext.RuntimeDelegateImpl at
net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:53)
at
net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:27)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at
java.lang.Class.forName0(Native Method) at
java.lang.Class.forName(Class.java:264) at
javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:62) at
javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:155) at
javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:105)
... 26 more
Code:
package me.singulativ.restapi.main;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import net.md_5.bungee.api.plugin.Plugin;
import java.io.IOException;
public class RestAPI extends Plugin {
private HttpServer server;
#Override
public void onEnable() {
try {
String host = "http://localhost:12345/";
server = HttpServerFactory.create(host);
server.start();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void onDisable() {
server.stop(0);
}
}
Dependencies
Artifacts

java.lang.NoClassDefFoundError in ubuntu terminal

I am crating a cron scheduler for which i am using the following code:-
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.ScheduleBuilder;
public class CronSched {
public static void main(String[] args) throws SchedulerException {
System.out.println("Started");
JobDetail job = JobBuilder.newJob(CronJob.class).build();
Trigger t1 = TriggerBuilder.newTrigger().withIdentity("MAILER").withSchedule(CronScheduleBuilder.cronSchedule("0 33 2 1/1 * ? *")).build();
Scheduler sc = StdSchedulerFactory.getDefaultScheduler();
sc.start();
sc.scheduleJob(job,t1);
}
}
and I am compiling teh following code in terminal with the following statement:-
javac -cp ".:/home/aditya/CronSched/lib/quartz-2.2.3.jar: /home/aditya/CronSched/lib/slf4j-api-1.7.25.jar: /home/aditya/CronSched/lib/slf4j-simple-1.7.25.jar: " CronSched.java
and on running the code I am using the following statement:-
java CronSched
and I am getting the following error:-
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/quartz/ScheduleBuilder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.quartz.ScheduleBuilder
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
What to Do?
You have to add all necessaries jars to your runtime classpath.
java -cp "quartz-2.2.3.jar:...." CronSched

Caused by: java.lang.ClassNotFoundException: Log

When doing a remote EJB lookup (from JBoss 4.3) in J2SE, I'm encountering the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: LLog;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2499)
at java.lang.Class.getDeclaredField(Class.java:1951)
at java.io.ObjectStreamClass.getDeclaredSUID(ObjectStreamClass.java:1659)
at java.io.ObjectStreamClass.access$700(ObjectStreamClass.java:72)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:480)
at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:468)
at java.security.AccessController.doPrivileged(Native Method)
at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:468)
at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:365)
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:602)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1622)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1517)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1706)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1344)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at java.rmi.MarshalledObject.get(MarshalledObject.java:159)
at org.jnp.interfaces.MarshalledValuePair.get(MarshalledValuePair.java:72)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:737)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:654)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.example.ShoppingBasketServiceClient.main(ShoppingBasketServiceClient.java:44)
Caused by: java.lang.ClassNotFoundException: Log
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 32 more
Here is teh codez, line 44 is the one doing the lookup:
package com.example;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ShoppingBasketServiceClient {
public static void main(String[] args) throws NamingException {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
String Lookup = "ejb/ShoppingBasketService/remote";
try {
InitialContext ic = new InitialContext(props);
// ShoppingBasketServiceRemote shoppingBasketService = (ShoppingBasketServiceRemote) ic.lookup(Lookup);
Object shoppingBasketService = (Object) ic.lookup(Lookup);
} catch (NamingException e) {
System.out.println("Exception:" + e);
}
}
}
I'm confused as to why it's looking for a Log class (and why the exception is not package-qualified)?
ShoppingBasketServiceRemote is just an interface that imports the javax.ejb.Remote annotation and extends another interface - neither make any reference to a Log class? Additionally, you'll see my example only casts the result to Object (but the result is the same if I swap out for the service interface)?
ShoppingBasketServiceRemote is just an interface that imports the
javax.ejb.Remote annotation and extends another interface - neither
make any reference to a Log class?
Examine all the objects referenced in the interface & their
transitive dependencies. Something will point to the "Log" class.
Also check the remote server for errors, maybe the error is on the
remote server
Try doing a simple
Class.forName("ShoppingBasketServiceRemote") - you might see it failing with the same error.
Additionally, you'll see my example only casts the result to Object
(but the result is the same if I swap out for the service interface)?
Casting the object happens after your jvm is able to load the object returned by ic.lookup(Lookup) - so the error will happen whether you cast or not.

Error parsing JSON in Java: ClassNotFoundException

I'm trying to parse a bit of JSON in Java by following this tutorial. Because it mentions nothing about org.apache.commons.io.IOUtils other than it imports it, I figured all it was used for it to convert an InputStream into a String. I have my own code for that, so I omitted it. Here is my code:
import java.io.*;
import java.io.InputStream;
import java.net.*;
import java.util.Scanner;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
public class Net {
static String returnParsedIp() throws UnsupportedEncodingException, MalformedURLException, IOException {
String url = "http://httpbin.org/ip";
URLConnection connection = new URL(url).openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
InputStream response = connection.getInputStream();
String responseString = new Scanner(response,"UTF-8").useDelimiter("\\A").next();
//return responseString;
JSONObject json = (JSONObject) JSONSerializer.toJSON(responseString);
String Ip = json.getString("origin");
return Ip;
}
}
I then call the method from my main class with:
public class DDNS {
public static void main(String[] args) throws Exception {
String Ip = Net.returnParsedIp();
System.out.println(Ip);
}
}
However, it throws the following error:
run:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at ddns.Net.returnParsedIp(Net.java:19)
at ddns.DDNS.main(DDNS.java:6)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 14 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Any help is appreciated.
Either you don't have the Apache Commons lang and io JARs in your CLASSPATH or you have the wrong version.
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
This is Java 101. You need all the dependencies in the CLASSPATH in order to compile and run.
You have to add the commons-lang jar in yout project

Gdata API blogger example

I'm having trouble running this sample code for the gdata java library:
import java.net.URL;
import com.google.gdata.client.Query;
import com.google.gdata.client.blogger.BloggerService;
import com.google.gdata.data.Feed;
public class AccessBloggerSample {
public static void main(String[] args) throws Exception {
BloggerService service = new BloggerService("Testing-Blogger");
service.setUserCredentials("tmerachli#gmail.com", "XXX");
System.out.println("Google: " + BloggerService.getVersion());
URL feedUrl = new URL("http://minhchaunyc.blogspot.com/feeds/posts/default ");
Query query = new Query(feedUrl);
System.out.println("service: ");
Feed resultFeed = service.getFeed(query, Feed.class);
System.out.println("blogs: " + resultFeed.getEntries().size());
}
}
And I get the following error:
java.lang.NoClassDefFoundError: com/google/common/collect/Maps
at com.google.gdata.wireformats.AltRegistry.<init>(AltRegistry.java:118)
at com.google.gdata.wireformats.AltRegistry.<init>(AltRegistry.java:100)
at com.google.gdata.client.Service.<clinit>(Service.java:555)
at AccessBloggerSample.main(AccessBloggerSample.java:23)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.Maps
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at com.google.gdata.wireformats.AltRegistry.<init>(AltRegistry.java:118)
at com.google.gdata.wireformats.AltRegistry.<init>(AltRegistry.java:100)
at com.google.gdata.client.Service.<clinit>(Service.java:555)
at AccessBloggerSample.main(AccessBloggerSample.java:23)
at __SHELL0.run(__SHELL0.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at bluej.runtime.ExecServer$3.run(ExecServer.java:724)`
Any ideas why I'm getting this? I'm compiling and running my code through BlueJ and I extracted all the jar files into the lib directory of BlueJ and the code compiles.
You need to add guava to your Java Build-Patproject: http://code.google.com/p/guava-libraries/

Categories