My Main
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
System.out.println("hola");
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:201)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:551)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.
DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:108)
... 7 more
The NullPointerException error is at almost impossible location:
static {
ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
try {
javaxInjectProviderClass = cl.loadClass("javax.inject.Provider"); /* line 108 */
} catch (ClassNotFoundException ex) {
// JSR-330 API not available - Provider interface simply not supported then.
}
}
This means that the class is not able to get its own classloader. You must have done something really bad to get this error. Check your JRE/JDK, IDE, ...
UPDATE
There is no explanation other than that you are probably trying to put Spring JARs into JRE's library folder (${java.home}/jre/lib). If that is the case, that is simply wrong. If you really want to include external JARs within JRE, then put them in the official extension directory - ${java.home}/jre/lib/ext.
Related
It's very interesting to use Class.forName() to load a class and get errors... I write one line code and get an error as above.
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream"));
}
}
When I use java UnsatisfiedLinkErrorTest, I get an error:
> java UnsatisfiedLinkErrorTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.net.SocketOutputStream.init()V
at java.net.SocketOutputStream.init(Native Method)
at java.net.SocketOutputStream.<clinit>(SocketOutputStream.java:44)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at UnsatisfiedLinkErrorTest.main(UnsatisfiedLinkErrorTest.java:3)
But when I run it in Idea, I get:
> java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49247:/Applications/IntelliJ IDEA.app/Contents/bin" UnsatisfiedLinkErrorTest
class java.net.SocketOutputStream
I don't know why in Idea I can get the true answer, (maybe jni?) I hope someone can tell me the reason. Thanks.
This question has been solved. I then cancelled the initialization of the class, then the error goes. The error is about the initialization, but not about finding the native library. So:
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream", false, null)); // will be okay!
}
}
I am using Java 1.7 with neo4j-community-2.0-1.1 to build a sample neo4j graph database. Please see below my code
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class showData {
private static final String Neo4J_DBPath = "/Technology/neo4j-community-2.0-1.1";
/**
* #param args
*/
Node first;
Node second;
Relationship relation;
GraphDatabaseService graphDataService;
//List of relationships
private static enum RelationshipTypes implements RelationshipType
{
KNOWS
}
public static void main(String[] args)
{
showData data = new showData();
data.createDatabase();
data.removeData();
data.shutDown();
}
void createDatabase()
{
//GraphDatabaseService
graphDataService = new GraphDatabaseFactory().newEmbeddedDatabase(Neo4J_DBPath);
// Begin transaction
Transaction transaction = graphDataService.beginTx();
try
{
// create nodes and set the properties the nodes
first = graphDataService.createNode();
first.setProperty("Name", "Ravneet Kaur");
second = graphDataService.createNode();
second.setProperty("Name", "Harpreet Singh");
//specify the relationships
relation = first.createRelationshipTo(second, RelationshipTypes.KNOWS);
relation.setProperty("relationship-type", "knows");
//success transaction
System.out.println(first.getProperty("name").toString());
System.out.println(relation.getProperty("relationship-type").toString());
System.out.println(second.getProperty("name").toString());
transaction.success();
}
finally
{
transaction.finish();
}
}
void removeData()
{
Transaction transaction = graphDataService.beginTx();
try
{
first.getSingleRelationship(RelationshipTypes.KNOWS,Direction.OUTGOING).delete();
System.out.println("Nodes are deleted");
//delete the nodes
first.delete();
second.delete();
transaction.success();
}
finally
{
transaction.finish();
}
}
void shutDown()
{
graphDataService.shutdown();
System.out.println("Database is shutdown");
}
}
Earlier I was using Jave 1.6 to compile this code, but got to know that this neo4j jar complies with jdk 1.7. So I changed it to JDK 1.7 and made all necessary changes in Installed JRE, Execution Environments and Java Build Path in eclipse to point to latest java.
Now I get the following error
Exception in thread "main" java.lang.RuntimeException: Error starting org.neo4j.kernel.EmbeddedGraphDatabase, /Technology/neo4j-community-2.0-1.1
at org.neo4j.kernel.InternalAbstractGraphDatabase.run(InternalAbstractGraphDatabase.java:330)
at org.neo4j.kernel.EmbeddedGraphDatabase.<init>(EmbeddedGraphDatabase.java:63)
at org.neo4j.graphdb.factory.GraphDatabaseFactory$1.newDatabase(GraphDatabaseFactory.java:92)
at org.neo4j.graphdb.factory.GraphDatabaseBuilder.newGraphDatabase(GraphDatabaseBuilder.java:198)
at org.neo4j.graphdb.factory.GraphDatabaseFactory.newEmbeddedDatabase(GraphDatabaseFactory.java:69)
at com.PNL.data.neo4j.showData.createDatabase(showData.java:45)
at com.PNL.data.neo4j.showData.main(showData.java:34)
Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.impl.transaction.XaDataSourceManager#7594035c' was successfully initialized, but failed to start. Please see attached cause exception.
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:509)
at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:115)
at org.neo4j.kernel.InternalAbstractGraphDatabase.run(InternalAbstractGraphDatabase.java:307)
... 6 more
Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource#24367e26' was successfully initialized, but failed to start. Please see attached cause exception.
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:509)
at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:115)
at org.neo4j.kernel.impl.transaction.XaDataSourceManager.start(XaDataSourceManager.java:164)
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:503)
... 8 more
Caused by: org.neo4j.kernel.impl.storemigration.UpgradeNotAllowedByConfigurationException: Failed to start Neo4j with an older data store version. To enable automatic upgrade, please set configuration parameter "allow_store_upgrade=true"
at org.neo4j.kernel.impl.storemigration.ConfigMapUpgradeConfiguration.checkConfigurationAllowsAutomaticUpgrade(ConfigMapUpgradeConfiguration.java:39)
at org.neo4j.kernel.impl.storemigration.StoreUpgrader.attemptUpgrade(StoreUpgrader.java:71)
at org.neo4j.kernel.impl.nioneo.store.StoreFactory.tryToUpgradeStores(StoreFactory.java:144)
at org.neo4j.kernel.impl.nioneo.store.StoreFactory.newNeoStore(StoreFactory.java:124)
at org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource.start(NeoStoreXaDataSource.java:323)
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:503)
... 11 more
BTW: Also my neo4j configuration parameter "allow_store_upgrade" is set to "true".
Any help will be really appreciated.
Regards
In your code the configuration is not picked up. To change this use the following snippet to initialize your db:
GraphDatabaseService graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(Neo4J_DBPath)
.loadPropertiesFromFile("confdir/neo4j.properties")
.newGraphDatabase();
Make sure neo4j.properties contains allow_store_upgrade=true. Alternatively you can use the deprecated setConfig(name, value) on the factory.
I have a class that compiles without error. The class has a main method. But when I try to run it on ubuntu linux from the classes' directory I get a class not found error. I am pretty sure I am missing something dead obvious but I don't see it.
Here is my ls operation:
zookeeper#zookeeper-virtual-machine:~/zookeeper-3.4.5/programs$ ls
CreateGroup.java LsGroup.class LsGroup.java zookeeper-3.4.5.jar
Here is what happens when I to run LsGroup
zookeeper#zookeeper-virtual-machine:~/zookeeper-3.4.5/programs$ java -cp "zookeeper-3.4.5.jar" LsGroup
Exception in thread "main" java.lang.NoClassDefFoundError: LsGroup
Caused by: java.lang.ClassNotFoundException: LsGroup
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: LsGroup. Program will exit.
Here is the code for LsGroup
package org.zookeeper;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;
public class LsGroup implements Watcher {
private static final int SESSION_TIMEOUT = 5000;
private ZooKeeper zk;
private CountDownLatch connectedSignal = new CountDownLatch(1);
public void connect(String hosts) throws IOException, InterruptedException {
zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
connectedSignal.await();
}
#Override
public void process(WatchedEvent event) { // Watcher interface
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
public void ls(String groupName) throws KeeperException, InterruptedException {
String path = "/" + groupName;
try {
List<String> children = zk.getChildren(path, false);
for (String child : children) {
System.out.println(path+"/"+child);
System.out.println(zk.getChildren(path +"/"+ child, false));
}
} catch (KeeperException.NoNodeException e) {
System.out.printf("Group %s does not exist\n", groupName);
System.exit(1);
}
}
public void close() throws InterruptedException {
zk.close();
}
public static void main(String[] args) throws Exception {
LsGroup lsGroup = new LsGroup();
lsGroup.connect(args[0]);
lsGroup.ls(args[1]);
lsGroup.close();
}
}
The original problem was that your class was in a package, but you were trying to load it as if it weren't in a package. You'd normally organize your source code to match your package hierarchy, then from the root of the hierarchy, you'd run something like:
java -cp .:zookeeper-3.4.5.jar org.zookeeper.LsGroup
Now that you've temporarily worked around the package issue by moving the code out of a package, the next problem is that the current directory isn't in the classpath. So instead of this:
java -cp "zookeeper-3.4.5.jar" LsGroup
You want:
java -cp .:zookeeper-3.4.5.jar LsGroup
Once you've got that working, you should move the classes back into packages, as per normal Java best practice.
You could remove the package declaration:
package org.zookeeper;
...or just place your LsGroup class in org/zookeeper directory.
The class file is supposed to live in a path like:
org/zookeeper/LsGroup.class
The -cp must include the directory that contains the org/ directory. Then you can
java -cp parent-of-org org.zookeeper.LsGroup
The message "wrong name: org/zookeeper/LsGroup" means, you have to respect the package structure of Java. Use the following directory structure:
./org/zookeeper/LsGroup.class
Then launch java org.zookeeper.LsGroup from within the current directory. The package separator "." will be translated to corresponding directory.
Your files are not under package org.zookeeper
You should be running your class from ~/zookeeper-3.4.5/org/zookeeper
Otherwise JVM won't find the classes to load.
Your class is part of the org.zookeeper package but you keep it in the root folder of your project (/zookeeper-3.4.5/programs).
The qualified name of the package member and the path name to the file are parallel (see Managing Source and Class Files), so if your package is org.zookeeper the class file should be kept in /zookeeper-3.4.5/programs/org/zookeeper
You made two mistakes:
1) You tried to run java LsGroup but you have to use the complete name including packages java org.zookeeper.LsGroup
2) your directory structure is not correct: the package org.zookeeper corresponds with the directory structure ./org/zookeeper/
If you change the directory structure and then run java from the top of this directory structure it should work
I am following the example listed in this link to add custom email notifications.
In my maven project 'RegistryService': I created the EmailTransformHandler and used maven to create the RegistryService-0.1.jar. I also modified the GREG_HOME/repository/conf/axis2/axis2.xml by adding the handler:
<handler name="EmailTransformHandler"
class="com.registration.example.service.EmailTransformHandler"/>
and dropped the RegistryService-0.1.jar in GREG_HOME/repository/components/dropins
package com.registration.example.service;
import java.util.ArrayList;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.xpath.AXIOMXPath;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.engine.Handler;
import org.apache.axis2.handlers.AbstractHandler;
public class EmailTransformHandler extends AbstractHandler implements Handler {
private String name;
public String getName() {
return name;
}
#Override
public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
if (msgContext.getTo() != null && msgContext.getTo().getAddress().startsWith("mailto:")) {
try {
SOAPEnvelope env = msgContext.getEnvelope();
AXIOMXPath xPath = new AXIOMXPath("//ns:text");
xPath.addNamespace("ns", "registry_example");
OMElement element = (OMElement) ((ArrayList) xPath.evaluate(env)).get(0);
element.setText(element.getText().replace("--", "This message intercepted by Terminator"));
} catch (Exception e) {
e.printStackTrace();
}
}
return InvocationResponse.CONTINUE;
}
}
When I go to the GREG_HOME/bin and start the server I get following exception:
2013-02-08 12:05:00,952] FATAL {org.wso2.carbon.core.init.CarbonServerManager} - WSO2 Carbon initialization Failed
org.apache.axis2.AxisFault: Exception occured while loading the Axis configuration from GREG_HOME/wso2/wso2greg-4.5.3/repository/conf/axis2/axis2.xml
at org.wso2.carbon.core.CarbonAxisConfigurator.getAxisConfiguration(CarbonAxisConfigurator.java:190)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:64)
at org.wso2.carbon.core.CarbonConfigurationContextFactory.createNewConfigurationContext(CarbonConfigurationContextFactory.java:65)
at org.wso2.carbon.core.init.CarbonServerManager.initializeCarbon(CarbonServerManager.java:398)
at org.wso2.carbon.core.init.CarbonServerManager.removePendingItem(CarbonServerManager.java:290)
at org.wso2.carbon.core.init.PreAxis2ConfigItemListener.bundleChanged(PreAxis2ConfigItemListener.java:118)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.dispatchEvent(BundleContextImpl.java:847)
at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: org.apache.axis2.deployment.DeploymentException: com.registration.example.service.EmailTransformHandler
at org.apache.axis2.deployment.util.Utils.loadHandler(Utils.java:149)
at org.apache.axis2.deployment.AxisConfigBuilder.processPhaseList(AxisConfigBuilder.java:549)
at org.apache.axis2.deployment.AxisConfigBuilder.processPhaseOrders(AxisConfigBuilder.java:584)
at org.apache.axis2.deployment.AxisConfigBuilder.populateConfig(AxisConfigBuilder.java:150)
at org.wso2.carbon.core.CarbonAxisConfigurator.populateAxisConfiguration(CarbonAxisConfigurator.java:308)
at org.wso2.carbon.core.CarbonAxisConfigurator.getAxisConfiguration(CarbonAxisConfigurator.java:188)
... 8 more
Caused by: java.lang.ClassNotFoundException: com.registration.example.service.EmailTransformHandler
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:513)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:429)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:417)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.axis2.util.Loader.loadClass(Loader.java:261)
at org.apache.axis2.util.Loader.loadClass(Loader.java:229)
at org.apache.axis2.deployment.util.Utils.loadHandler(Utils.java:116)
... 13 more
What configuration am I missing? Googling about this error does not yield any relevant results( except some Russian forums which do not help me much). I even tried adding the jar via WSO2 Management Console as an extension, but I still get this exception.
According to the error, your RegistryService-0.1.jar is not set properly to the class path.
If the jar file is not an OSGI bundle that jar file should be drop in to the "GREG_HOME/repository/components/lib" directory.
-Ajith
I have a class that is having some dependency problems referencing external library files. Every time I try to run this on the server I get errors saying class not found, such as this:
SEVERE: Class [ org/json/JSONException ] not found. Error while loading [ class com.myproj.logic.Driver ]
this is preventing the class from executing. I tried taking out the specific throws execption by just saying "throws exception" and got the following error:
WARNING: A system exception occurred during an invocation on EJB Driver method public void com..logic.Driver.initURL() throws java.lang.Exception
javax.ejb.EJBException: javax.ejb.CreateException: Initialization failed for Singleton Driver
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
#Singleton
public class Driver {
#EJB RSSbean rssbean;
#PostConstruct
#Schedule(hour ="*/1", minute ="*/1", second ="*/1", persistent = false)
public void initURL() throws IOException, JSONException{
URL twitterSource = new URL("http://search.twitter.com/search.json?q=news");
ByteArrayOutputStream urlOutputStream = new ByteArrayOutputStream();
IOUtils.copy(twitterSource.openStream(), urlOutputStream);
String urlContents = urlOutputStream.toString();
JSONObject thisobject = new JSONObject(urlContents);
JSONArray names = thisobject.names();
JSONArray asArray = thisobject.toJSONArray(names);
JSONArray resultsArray = thisobject.getJSONArray("results");
JSONObject jsonObject = resultsArray.getJSONObject(0);
String twitterText = jsonObject.getString("text");
//System.out.println(twitterText);
System.out.println("Calling rssbean from Driver");
rssbean.updateDatabase("twitterText");
}
}
I have edited the Java classpath and added a user library for each of these as well as editing the Build Path of the project. The libraries are displayed in the list and I don't get compiler errors so at least Eclipse has recognized them. The problem comes during execution so I think something is wrong there.
Should I edit the classpath in Windows>Preferences>Java>Classpath> and add the Jars there? I have not had to do this for any other libraries before.
I found out you have to add any library files in the Glassfish/domain/lib directory before they're recognized by glassfish on the server.