Java/eclipse/hibernate - java

I'm a .net dev, usually do this using nhibernate and can work with that fine. I'm doing a project which needs to be written in Java and have the following issue:
All I have so far is a blank java project setup in eclipse. I have C:\work\lib\java\cp\hibernate3.jar in the Referenced Libraries node in the package explorer.
in my code (main) i'm doing this
Configuration config = new Configuration().
setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect").
setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver").
setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:RefenceDb").
setProperty("hibernate.connection.username", "sa").
setProperty("hibernate.connection.password", "").
setProperty("hibernate.connection.pool_size", "1").
setProperty("hibernate.connection.autocommit", "true").
setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider").
setProperty("hibernate.hbm2ddl.auto", "create-drop").
setProperty("hibernate.show_sql", "true").
addClass(Boy.class);
and in the consol i have:
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at ReferenceApplication.Main.main(Main.java:15)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
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
any ideas?
the path C:\work\lib\java\cp\ is in the classpath
w://

While the other answers here referencing dom4j are correct, you will soon find there a dozen or so other dependencies.
Starting from total scratch is a noble pursuit, but I would actually recommend giving yourself a head start by installing Maven (for dependency management) and running mvn archetype:generate from the command line. If you're doing a web application (which you may not be based on the question text) selecting "maven-archetype-j2ee-simple" or "maven-archetype-webapp" would be a pretty good kickstart. Doing so will set up a project for you that can then be used in eclipse (you could even use m2eclipse plugin to do dependency management from within eclipse).
It will turn out that Maven has a learning curve of its own, but my opinion is that the benefits outweigh the costs.
Good luck!

Download dom4j and put it on your classpath (in the referenced libraries tab)

From the stack trace, it looks like you're missing dom4j.jar. Hibernate (for Java) has a ton of dependencies. Make sure that you have all of Hibernates required dependencies added to your Eclipse project.

You need to put the dom4j jar on your classpath. You should be able to get it here:
http://www.dom4j.org/download.html
I think that hibernate uses version 1.6.1

To make all this easier, I would quickly learn Maven and then use the m2eclipse plugin.
I know it's a learning curve, but it will make everything massively easier at deploy time.

Related

From command line - java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

OK. I know there are other questions like this one out there and this is not the first time that slf4j has kicked my butt. However, I have looked at my PATH in Environment Variables and below are the two slf4j jar files included in my PATH as well as my project dependencies.
C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar
C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar
This is what is in my pom file:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-ext</artifactId>
<version>1.7.13</version>
</dependency>
Other applications we are running contain slf4j jar dependencies in the IDE but they are not listed in the pom file. I am so confused about where to put what that I can't see straight.
This is what the application dependencies look like:
I can run the application from the IDE (Netbeans) but I get the following error when I try to run from command prompt.
C:\Users\pdl\Projects\WeatherTestDrive>java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar com.a2i.weatherclient.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.a2i.weatherclient.Client.<clinit>(Client.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 1 more
Adding slf4j in my VM classpath and I still get the error.
C:\Users\pdl\Projects\WeatherTestDrive>java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.weatherclient.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.a2i.weatherclient.Client.<clinit>(Client.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 1 more
Can somebody please help me figure out what I am doing wrong?
Should I be adding slf4j-api to my dependencies instead of slf4j-exe? Or even something else?
I guess whichever one I use, I should add it to my VM classpath.
Does it even need to be in my pom file?
------------------------------ EDIT ----------------------------------
I created a simple HelloWorld app to log my name. As soon as I added the Logger to my Hello class it was highlighted in red, so I added the slf4j-simple to my dependencies and slf4j-api came with it. But when I opened the pom file only the slf4j-simple was added:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.13</version>
</dependency>
When I run from the IDE, everything works well. But when I run from command line I still get the error:
C:\Users\pdl\Projects\HelloWorld\target>java -cp HelloWorld-1.0-SNAPSHOT.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.helloworld.Hello
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.a2i.helloworld.Hello.<clinit>(Hello.java:17)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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)
... 1 more
Where's slf4j in your VM classpath? Try running as follows:
java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.weatherclient.Client
You're missing slf4j from your runtime. Hope that helps.
Maven, and thus the pom-file, are intended to build the classpath for you.
So yes, all depedencies you like to use should be in your pom file.
Regarding slf4j:
slf4j-api is a dependency that only defines an api (or interface). To make it work you also have to add an implementation. See here for explanation. So you have to add at least one more dependency. For example:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
Regarding slf4j-ext, i am quite sure you dont need it to simply log. Maybe you have more elaborate use cases.
To start your application from command line you have to run java with the -cp argument. The classpath is a list all used classes or archives containing classes. For applications with a lot of dependencies it can become quiet cumbersome to build it manually.
Use mvn dependency:build-classpath to let maven build this big string of jar-paths for you.
This concerns Java SE projects built in IntelliJ where slf4j - or any other library for that matter - is included (i.e. slf4j-api-1.7.26.jar => The API & slf4j-simple-1.7.26.jar => the API implementation)...
Step #1:
create folder \libs in project root and place the two JARs in there click to see project structure
step #2:
Build code and make sure that the Logger is working properly inside the IDE.
step #3:
Launch cmd and navigate using cd command inside the .\out directory up until the packages
of the main (check 1st line of Main.java). i.e.
cd C:\Users.....\out...\testcp
step #4
Do right click on sjf4j-api jar and copy path (path#1) &
Do right click on sjf4j-simple jar and copy path (path#2) &
Do right click on Main.class and copy path (path#3. SOS: EXCLUDE THE Main.class part).
Now, run java command with -cp flag as:
java -cp path#1;path#2;path#3; org.me.Main
That's it
Note: Eclipse users do not run into this problem since this is taken care of by using the build path utility.

Create a jar file using jdev that includes weblogic.jar

I've been having troubles lately in creating a jar file that can call a secured web service on Weblogic server using Jdev.
I've created a web service proxy which is handling the situation perfectly. My goal is to deploy this web service as a jar file so that I can use it in my other projects as a simple library.
I was able to deploy the project as a jar file, which in turn allowed me to use it's different methods to connect to the web service.
However, when I run the web service client on eclipse I get an error:
Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/xml/crypto/wss/provider/CredentialProvider
at WebServiceCaller.callGetCardDetailJar(WebServiceCaller.java:55)
at WebServiceCaller.main(WebServiceCaller.java:29)
Caused by: java.lang.ClassNotFoundException: weblogic.xml.crypto.wss.provider.CredentialProvider
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)
... 2 more
The problem may be fixed by finding the weblogic.jar file and including it in the eclipse build path, but is there a way to deploy a jar file with all the library dependencies included in it ?
There is a way to create a wlfullclient.jar (see oracle doc http://docs.oracle.com/cd/E13222_01/wls/docs103/client/jarbuilder.html ), but your class is not in it, it's rather in oracle.webservices.standalone.client.jar or in wls-api.jar or in weblogic.jar... it's rather confusing, I think Oracle never managed to simplify this jar dependency problem, actually in earlier versions of WebLogic things were a lot simpler!

NoClassDefFoundError when using JAX-WS in an OSGI 4.2 bundle

I've been tasked with updating a plugin for Cytoscape, a biological visualization software platform, to the latest version of the Cytoscape API. Cytoscape 3.x uses an OSGI framework (Karaf 2.2.x, I think) to interface with its plugins (which are now called "apps").
The problem is that the plugin/app uses JAX-WS to communicate with an external server, and JAX-WS seems to have problems with loading classes in the OSGI environment.
Here is a snippet of the problematic code:
public class AnatServerService extends Service {
#WebEndpoint(name = "AnatServerPort")
public AnatServerIfc getServerPort() {
AnatServerIfc port = super.getPort(new QName("network", "AnatServerPort"), AnatServerIfc.class);
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, path);
return port;
}
}
And here is the resulting exception:
java.lang.NoClassDefFoundError: com.sun.xml.internal.ws.api.message.Header not found by AnatApp [168]
at com.sun.proxy.$Proxy64.<clinit>(Unknown Source)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.reflect.Proxy.newInstance(Unknown Source)
at java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.xml.internal.ws.client.WSServiceDelegate.createProxy(UnknownSource)
at com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBaseProxy(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)
at javax.xml.ws.Service.getPort(Unknown Source)
at anat.ws.AnatServerService.getServerPort(AnatServerService.java:36)
at anat.task.AvailableNetworksTask.getAvailableNetworks(AvailableNetworksTask.java:39)
at anat.task.AvailableNetworksTask.run(AvailableNetworksTask.java:62)
at org.cytoscape.work.internal.sync.SyncTaskManager.execute(SyncTaskManager.java:86)
at anat.view.BackgroundDefinitionDialog$AvailableNetworksSwingWorker.doInBackground(BackgroundDefinitionDialog.java:1544)
at anat.view.BackgroundDefinitionDialog$AvailableNetworksSwingWorker.doInBackground(BackgroundDefinitionDialog.java:1535)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I can confirm that this code does work outside of OSGI.
Any suggestions? I've tried experimenting with embedding the JAX-WS API and/or implementation classes directly into the bundle using Embed-Dependency, but that didn't help. I've also tried playing with the org.osgi.framework.system.packages.extra and org.osgi.framework.bootdelegation properties, to no avail. It is possible that I'm doing something wrong, though.
I'm afraid that OSGI might have some fundamental incompatibility with the Reflection API being used to create that header. But surely it can't be impossible to run a web service client in this environment, right?
It seems like JAX-WS is dynamically weaving dependencies into your bundle that were not present at build time. Because these dependencies are dynamic, the build tooling doesn't find them and doesn't generate an Import-Package statement for them.
Specifically your bundle has a dependency on the package com.sun.xml.internal.ws.api.message. You never wanted or asked for that dependency, but JAX-WS added it for you anyway. How nice of it!
Your question suggests that you are using Maven with the maven-bundle-plugin to build your bundle. Therefore you need to add something like this to your pom:
<Import-Package>
com.sun.xml.internal.ws.api.message,
*
</Import-Package>
Note that there may be other packages that need to be added to this list... you will probably find out about them after adding this one. Again, because these are dynamically weaved-in dependencies it's impossible to get a full list of them in advance.
With regard to your final question. You're right, it's certainly not impossible to run a web service client in this environment! However OSGi does tend to expose the invalid assumptions and bad coding practices that are typically found in crappy libraries like JAX-WS.
I've solved my own problem. It turns out I had edited the org.osgi.framework.bootdelegation property in the wrong file - config.properties instead of custom.properties.
This is still a problem in the long term, though. I'd like to be able to distribute this bundle without requiring users to edit config files.
I had the same problem. Adding the following line to Felix's config.properties file solved the problem:
org.osgi.framework.bootdelegation=com.sun.xml.internal.ws.*

NoClassDefFoundError in Osgi w/ Felix, ant

After a lot of work migrating our code to another & fixing all the manifests and the bundles,
when I try to run the applet on the server I get many errors like this one:
java.lang.NoClassDefFoundError: org/jitsi/service/configuration/ConfigurationService
at net.java.sip.communicator.service.resources.AbstractResourcesService.<init>(AbstractResourcesService.java:127)
at net.java.sip.communicator.impl.resources.ResourceManagementServiceImpl.<init>(ResourceManagementServiceImpl.java:48)
at net.java.sip.communicator.impl.resources.ResourceManagementActivator.start(ResourceManagementActivator.java:36)
at org.apache.felix.framework.util.SecureAction$Actions.run(SecureAction.java:1243)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:620)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:1904)
at org.apache.felix.framework.Felix.startBundle(Felix.java:1822)
at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1192)
at org.apache.felix.framework.StartLevelImpl.run(StartLevelImpl.java:266)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.jitsi.service.configuration.ConfigurationService not found by [12515]
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:812)
at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.felix.framework.ModuleImpl.getClassByDelegation(ModuleImpl.java:670)
at org.apache.felix.framework.resolver.WireImpl.getClass(WireImpl.java:102)
at org.apache.felix.framework.ModuleImpl.searchImports(ModuleImpl.java:1426)
at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:747)
at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:72)
at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1807)
at java.lang.ClassLoader.loadClass(Unknown Source)
All the bundles are getting resolved properly, and even when I tried to get some code out of the external jar into our code, it still threw me that error, I know that's a problem in Felix that I need to fix, but I don't know where can I start, all the bundles starting in their proper time...
I'm building the app with ant/Felix.
Maybe it's a problem with the activator?
Thanks for all the help, love to hear some opinions...
Your bundle is using the class org.jitsi.service.configuration.ConfigurationService but it doesn't import the package. Make sure that org.jitsi.service.configuration is listed in the Import-Package header in your bundle's manifest.

JUnit RemoteTestRunner NoClassDefFoundError Junit4 Eclipse 3.7.2

I am attemping to add JUnit tests to my existing Eclipse 3.7.2 project and it seems like JUnit is not properly added to the project. I have attempting adding the JUnit jar manually. I have tried several other run configuration modifications that all result in the same error. New projects that I create also exhibit this error when I add a JUnit test.
java.lang.NoClassDefFoundError: org/eclipse/jdt/internal/junit/runner/RemoteTestRunner
Caused by: java.lang.ClassNotFoundException: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner
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)
Exception in thread "main"
Are you installing Eclipse to a directory to which you can't write? Windows 7 UAC can prevent certain files from being written, especially in C:\Program Files.... It's better to install Eclipse in a directory that you can write.
This could be caused by a classpath issue. First ensure that you definitely have the junit.jar in your build path.
In Eclipse Indigo: Go to “Package Explorer” right-hand click > ‘Build Path’ > ‘Configure Build Path’ > in the Libraries tab. There you should the junit.jar.
Generally creating the test case within Eclipse adds this jar to the build path.
The same exception could also occur if you have a build path conflict. Going to Window > Show View > 'Markers' shows you the problems with the project. (In my case there was a circular reference)

Categories

Resources