I Am trying to compile very simple class with Janino:
import org.codehaus.commons.compiler.CompileException;
import org.codehaus.janino.ClassBodyEvaluator;
import java.io.IOException;
import java.io.StringReader;
public class JaninoTest{
public static void main(String[] args) throws IOException, CompileException {
String sampleClass =
"public class Test {" +
"public void sampleMethod() {\n" +
" JsonObject obj = new JsonObject();\n" +
" obj.add(\"p1\", new JsonPrimitive(2));\n" +
" System.out.println(obj.get(\"p1\"));\n" +
"}" +
"}";
ClassBodyEvaluator classBodyEvaluator = new ClassBodyEvaluator();
classBodyEvaluator.setParentClassLoader(Thread.currentThread().getContextClassLoader());
classBodyEvaluator.setDefaultImports(new String[] { "com.google.gson.JsonObject",
"com.google.gson.JsonPrimitive"});
StringReader sr = new StringReader(sampleClass);
classBodyEvaluator.cook(null, sr);
Class<?> clazz = classBodyEvaluator.getClazz();
System.out.println(clazz.getName());
}
}
Every things works fine here, but org.codehaus.janino.ClassBodyEvaluator compiler have limitation to Java 5.
So I read on Janino website that you can change compiler to overcome this limitation
JANINO can be configured to use javax.tools.JavaCompiler, i.e. JAVAC,
which removes the Java 5-related limitations.
So I changed ClassBodyEvaluator on this one org.codehaus.commons.compiler.jdk.ClassBodyEvaluator; which is also available on Janino website and AFAIK don't have Java 5 limitation.
But unfortunately it throws following stack trace:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/main/Main$Result
at com.sun.tools.javac.main.Main.compile(Main.java:581)
at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
at org.codehaus.commons.compiler.jdk.SimpleCompiler.cook(SimpleCompiler.java:128)
at org.codehaus.commons.compiler.jdk.ClassBodyEvaluator.cook(ClassBodyEvaluator.java:197)
at org.codehaus.commons.compiler.jdk.ClassBodyEvaluator.cook(ClassBodyEvaluator.java:108)
at JaninoTest.main(JaninoTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: com.sun.tools.javac.main.Main$Result
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more
This probably has something to do with compiler classpath but I don't see any method in API to somehow manipulate it.
I will be grateful for any suggestions
Works for me.
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
Related
I have randomly this problem when I try to initialize ehcache:
Caused by: org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] #PostConstruct private cerebro84.util.cache.CacheManagerServiceBean.initialize() on cerebro84.util.cache.CacheManagerServiceBean#3ee90439
at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:405)
at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.postConstruct(ManagedBean.java:178)
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:298)
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:103)
at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:90)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
at cerebro84.util.cache.CacheManagerServiceBean$Proxy$_$$_WeldClientProxy.getFromCache(CacheManagerServiceBean$Proxy$_$$_WeldClientProxy.java)
at cerebro84.util.cache.CacheMethodInterceptor.getResultFromCache(CacheMethodInterceptor.java:29)
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:267)
at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137)
at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:263)
at org.jboss.weld.introspector.jlr.WeldMethodImpl.invoke(WeldMethodImpl.java:168)
at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:403)
... 210 more
Caused by: java.lang.NoClassDefFoundError: net/sf/saxon/trans/XPathException
at net.sf.saxon.IdentityTransformerHandler.startDocument(IdentityTransformerHandler.java:110)
at com.sun.xml.bind.v2.runtime.unmarshaller.DomLoader$State.<init>(DomLoader.java:83)
at com.sun.xml.bind.v2.runtime.unmarshaller.DomLoader.startElement(DomLoader.java:118)
at com.sun.xml.bind.v2.runtime.unmarshaller.ProxyLoader.startElement(ProxyLoader.java:60)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:528)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:507)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:178)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:244)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:281)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:250)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:281)
at com.sun.xml.bind.unmarshaller.DOMScanner.visit(DOMScanner.java:250)
at com.sun.xml.bind.unmarshaller.DOMScanner.scan(DOMScanner.java:127)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:369)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:348)
at org.ehcache.xml.ConfigurationParser.<init>(ConfigurationParser.java:177)
at org.ehcache.xml.XmlConfiguration.parseConfiguration(XmlConfiguration.java:178)
at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:166)
at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:134)
at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:327)
at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:127)
at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:78)
at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:186)
at cerebro84.util.cache.CacheManagerServiceBean.initialize(CacheManagerServiceBean.java:38)
... 220 more
The weird behavior is that if I restarted the container (weblogic), it works fine. This is the class I use to initialize the cache, trimming down to the bare essential:
import javax.annotation.PostConstruct;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.enterprise.context.ApplicationScoped;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
#Slf4j
#ApplicationScoped
public class CacheManagerServiceBean {
private static final String EHCACHE_CONFIG_FILE_PATH = "/ehcache.xml";
#Getter
private CacheManager cacheManager = null;
#Getter
private boolean cacheEnabled = false;
#PostConstruct
private void initialize() {
logger.info("Instantiating cache manager");
try {
this.cacheManager = Caching.getCachingProvider().getCacheManager(
getClass().getResource(EHCACHE_CONFIG_FILE_PATH).toURI(),
getClass().getClassLoader());
cacheEnabled = true;
} catch (Exception|NoClassDefFoundError e) {
logger.error("Unable to enable cache", e);
}
}
}
I tried to add the following line
<wls:package-name>net.sf.saxon.*</wls:package-name>
to /META-INF/weblogic-application.xml, but this did't help. Saxon is a direct dependancy of the module:
<dependency>
<groupId>net.sf.saxon</groupId>
<artifactId>Saxon-HE</artifactId>
<version>9.5.1-8</version>
</dependency>
The module is packaged as jar in a ear multimodule application.
Does anybody have any idea on how to make sure that the class XPathException is always found by saxon? Thank you in advance.
Problems like this are usually caused by having several different versions of Saxon on the classpath at the same time.
currently, I try to use CloudRail with the last version of cloudrail-si-java (version: 2.21.1), but I get this error with Twitter API:
Exception in thread "main" java.lang.RuntimeException: ServiceCode
Error in function authenticate at 13 at
com.cloudrail.si.servicecode.Interpreter.run(Interpreter.java:111) at
com.cloudrail.si.servicecode.Interpreter.callFunction(Interpreter.java:45)
at com.cloudrail.si.services.Twitter.login(Twitter.java:909) at
com.whois.multiPlateformPosting.testDemo.main(testDemo.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497) at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.NullPointerException at
java.io.Reader.(Reader.java:78) at
java.io.InputStreamReader.(InputStreamReader.java:72) at
java.util.Scanner.(Scanner.java:563) at
com.cloudrail.si.servicecode.commands.stream.StreamToString.dumpStream(StreamToString.java:39)
at
com.cloudrail.si.servicecode.commands.stream.StreamToString.execute(StreamToString.java:33)
at com.cloudrail.si.servicecode.Interpreter.run(Interpreter.java:87)
... 8 more
My source code look like that:
import com.cloudrail.si.servicecode.commands.awaitCodeRedirect.LocalReceiver;
import com.cloudrail.si.services.Twitter;
public class testDemo {
public static void main(String args[]){
com.cloudrail.si.CloudRail.setAppKey("5----------19");
System.out.println("getAppKey: " + com.cloudrail.si.CloudRail.getAppKey());
com.cloudrail.si.CloudRail.validateKey();
Twitter service = new Twitter(
new LocalReceiver(8042),
"8KS-----1Hd",
"gT---kzTu",
"http://localhost:8042/auth"
);
service.login();
System.out.println("end" );
}
}
the identifiers come from this page:
How I can fixe it ?
Best regards,
I'm using jaxb inside a jboss 5.0.1GA container, and seeing a strange exception at startup. It appears once only.
From the stacktrace you can see that it is during the class initialisation of FrameworkUtil class.
FrameworkUtil line 119 looks like this:
JAXBContext j = JAXBContext.newInstance(Validate.class, Response.class ....<more classes>);
Validate.java has this in it:
#XmlRootElement(name="validate")
public class Validate {
List<String> userGroups;
#XmlElementWrapper(name="userGroups")
#XmlElement(name="item")
public String[] getUserGroups() {
if (userGroups != null) {
return userGroups.toArray(new String[userGroups.size()]);
}
else return null;
}
public void setUserGroups(String[] userGrps) {
userGroups = new ArrayList<String>();
if (userGrps != null) {
for (String userGrp : userGrps) {
userGroups.add(new String(userGrp));
}
}
}
}
In the stack trace is a reference to Validate. But sometimes the stacktrace is different, and refers to the same field (userGroups) in Response.class instead.
Here's the stack trace:
Mar 19, 2015 11:20:50 AM com.sun.xml.bind.v2.runtime.reflect.opt.Injector inject
WARNING: duplicate class definition bug occured? Please report this : com/mycompany/ecommerce/message/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;
java.lang.ClassFormatError: Illegal class name "com/mycompany/ecommerce/message/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;" in class file com/mycompany/ecommerce/messa
ge/beans/Validate$JaxbAccessorM_getUserGroups_setUserGroups_[Ljava_lang_String;
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
at sun.reflect.GeneratedMethodAccessor209.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:205)
at com.sun.xml.bind.v2.runtime.reflect.opt.Injector.inject(Injector.java:85)
at com.sun.xml.bind.v2.runtime.reflect.opt.AccessorInjector.prepare(AccessorInjector.java:89)
at com.sun.xml.bind.v2.runtime.reflect.opt.OptimizedAccessorFactory.get(OptimizedAccessorFactory.java:114)
at com.sun.xml.bind.v2.runtime.reflect.Accessor$GetterSetterReflection.optimize(Accessor.java:369)
at com.sun.xml.bind.v2.runtime.property.ArrayProperty.<init>(ArrayProperty.java:65)
at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.<init>(ArrayERProperty.java:84)
at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:96)
at com.sun.xml.bind.v2.runtime.property.ArrayElementLeafProperty.<init>(ArrayElementLeafProperty.java:66)
at sun.reflect.GeneratedConstructorAccessor172.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:124)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:179)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:515)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:330)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1140)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:154)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:121)
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:606)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:248)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:235)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:432)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:637)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at com.mycompany.global.er.util.FrameworkUtil.<clinit>(FrameworkUtil.java:119)
Did you check this bug out ?
Seems to be fixed in version 2.2.11 of jaxb implementation.
Maybe you could try and replace jaxb JAR files, they're probably in you server's lib, unless your webapp overwrites them in it's own lib folder.
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
Solution: Add the guava dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
I'm trying to use the json-schema-validator over here: https://github.com/fge/json-schema-validator .
I'm using the following maven dependency:
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.5</version>
</dependency>
Here is a class I'm trying:
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
public class JsonSchemaTest {
public static void main(String[] args) throws IOException {
JsonNode jsonNode = JsonLoader.fromString("{\"a\":1}");
}
}
And here is the error I'm receiving:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/io/Closer
at com.github.fge.jackson.JsonNodeReader.fromReader(JsonNodeReader.java:120)
at com.github.fge.jackson.JsonLoader.fromReader(JsonLoader.java:179)
at com.github.fge.jackson.JsonLoader.fromString(JsonLoader.java:192)
at com.cisco.webex.squared.flume.JsonSchemaTest.main(JsonSchemaTest.java:10)
Caused by: java.lang.ClassNotFoundException: com.google.common.io.Closer
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)
... 4 more
--- Edit:
If I change my maven version to 2.1.7, I can do JsonNode jsonNode = JsonLoader.fromString("{\"a\":1}"); but I cannot create the factory here without the same java.lang.NoClassDefFoundError: com/google/common/io/Closer error:
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.report.ProcessingReport;
public final class JsonSchemaTest {
public static void main(final String[] args) throws IOException, ProcessingException {
//JsonNode data = JsonLoader.fromString("{\"a\":1}");
final JsonNode data = JsonLoader.fromString("{\"a\":1}");
final JsonNode fstabSchema = JsonLoader.fromString("{\"type\":\"object\", \"properties\":{\"a\":{\"type\":\"number\"}}}");
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(fstabSchema);
ProcessingReport report;
report = schema.validate(data);
System.out.println(report);
}
}
You have to import guava library in your project.
http://www.java2s.com/Code/Jar/g/Downloadguava150rc1jar.htm
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/