Java EE Shared Libs? - java

I'm having trouble getting a shared library working in a Java EE environment.
In particular, the library contains some helpers for common JMS activities. However, as soon as the code enters a library function that requires anything from javax.jms it's triggering a ClassNotFound exception (claiming it can't find javax.jms.Message, for instance.)
How do I get around this? I'm placing the shared lib under $AS_HOME\lib\endorsed -- does it belong elsewhere? I have tried also packaging all the dependencies inside the jar; this doesn't seem to make much of a difference.
I am really hoping this is much more straightforward than it is seeming right now. In order just to get it working, I've refactored the helper into a helper-per-module within each component that requires the functionality. Note that this is working, so I'm thinking the issue at this point is just classpath problems? Thanks for any advice!

The best practice is to put the jms.jar in:
$AS_HOME/domains/<your domain>/lib
but you could always just place them in:
$AS_HOME/lib/
If you are not familiar with domains, take a quick glance at Concept of a GlassFish Domain

Related

java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object - Tomcat 8

I am getting this exception:
java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object;
at org.hibernate.ejb.Ejb3Configuration.<clinit>(Ejb3Configuration.java:142)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
Why? There are two jars containing the class Logger. The jars are:
-jboss-logging-spi and jboss-logging
Jboss-logging-spi.jar is a transitive dependency of the jbosscache-core.jar and unfortunately it gets loaded first by Tomcat 8.
What is the best way to fix this? Is it possible to control the order in which the .jars are loaded by Tomcat?
Additional information:
-The current .war file which I can't get it working on the new server(Ubuntu) it is working on the old server(OpenSuse).
What is the best way to fix this?
Eliminate the duplicate classes. Period. There's no other recommendation. Masking the problem through class loading order will have a similar problem pop up later, making maintenance harder next time.
One application should never contain two conflicting implementations (or even two identical implementations) of the same class. If one of the classes is on the (appserver-) global classpath: Remove it from there.
Explicit JAR-ordering can be accomplished in Tomcat via abusing the resources facility in Tomcat. I'm not going to give a complete answer, here, because (a) it's highly non-recommended and (b) you'd better really know what you are doing in order to do it.
So, briefly:
Read all about resources
Use <PreResources> to force a library or libraries to the front of the line
Reconsider whether you really want to do this, or just fix the initial problem

Design a Plugin based java application

I'm trying to make a plugin-based application which means, besides the basic framework, other functionalities should be added with a plugin style.
The majar problem of such system, i think, is how to load new plugins at the runtime. I don't want to recomplie the whole project when a new plugin installed. Just like Eclipse, after new installation of plugin, the user only need to restart it.
So my first idea about that is using java reflection to load the class at the runtime. there might be some plugin configuration files, the system reads them and load the plugin classes with reflection. Maybe i can use the spring framework as well, because its Inversion of Control is just match my request.
Another idea i'm investigating is using the ejb container. when the plugins are ejbs, i can just pack them into jar and deploy them in an ejb container, then i can use jndi to access them. but it only works when the plugin are ejbs.
anyway, i'm a rookie in system design. so i post this topic and want to hear of your opinions.
btw. is there any good book about system design you want to recommand?
thanks alot in advance!
You may first look at OSGI and his implementation (Equinox for Eclipse and Apache Felix) but it would be easier if you give us more information of what you are trying to do.
Plug-in to solve what problem?
Designers of frameworks usually have deep domain knowledge that informs their design choices. Frameworks come about after two or three attempts to solve some problem; the earlier implementations give clues about how to abstract what's important into a framework.
Eclipse solves the problem of an expandable IDE (poorly, in my opinion). EJBs are transactional, distributed components that run in a container.
You've given no indication of what kind of problem you want to solve. Until you do, your ideas won't get very far.

Running class in separate context

I'm trying to make a simple application that loads and runs some classes during runtime. For example, let's say I have this config:
module1.classpath=module1.jar,somelibs1.jar
module1.class=com.blabla.Module1
module2.classpath=module2.jar,somelibs2.jar
module2.class=com.blabla.Module2
Then I need to load libraries specified in module1.classpath and run the module1.class with that libraries loaded. Afterwards I need to load module2.classpath and run module2.class with those libraries.
How do I handle the case when somelibs1.jar and somelibs2.jar have the same classes inside? Basically I'd like to run module1.jar using exclusively somelibs1.jar and module2.jar using exclusively somelibs2.jar. How do I implement that?
I'm guessing I need to create a separate classloader for each of my classes and push the jars in that classloaders. However I'd appreciate some example or at least a confirmation that it is a right way to do that.
This seems to be a pretty good use case for OSGI. I would recommend using OSGI for this as everything you nees is provided by OSGI out-of-box.
But if for some reason you can't use OSGI, then what you need to do is to have a classloader for each module. Load the moduleX.class by a ClassLoaderX, and moduleX.classpath should be added in to ClassLoaderX's path. You can use a set of simple URLClassLoader for this.
Thanks for question. Very interesting.
It seems to you can't use several versions of the same class in one instance of JVM. I've never had this task and I don't know how to implement this.
But let's play. I don't know what is exotic application do you develop. May be you can run many JVMs and each JVM will have exclusive CLASSPATH.
Write application which can run (for example using Runtime.exec()) another JVM and make a conversation to it via some channel (may be network).

Eclipse: share code among multiple projects

I have different projects in Eclipse (java me midlets) which all need to share code. Currently I duplicate things, but this is not ideal.
What is the best way to do this?
Thanks!
EDIT:
I tried to add another project in the Build Path, but then, when running the Midlet in a Emulator, I get the following ERROR:
Uncaught exception java/lang/NoClassDefFoundError: clientlibrary/ov9292/Ov9292Client.
You can create a Java Project which will contain the shared code and have other projects reference it. When deploying you can pack this project as a separate jar file.
Have you considered creating a shared library instead of just shared code? Export to a JAR file and import that into the dependent products.
The up-side of doing this (vs just referencing the shared code) is that it will force you into more rigorous practices. You will now have an API that must be maintained and versioned cleanly. It will also make you better consider what's shared and what's not. This will likely lead to better encapsulation, and hopefully better testing.
I'm not saying you lack these things now. Just that creating a common-foo.jar can often encourage better habits.

Projects with browsable source using dependency injection w/ guice?

I often read about dependency injection and I did research on google and I understand in theory what it can do and how it works, but I'd like to see an actual code base using it (Java/guice would be preferred).
Can anyone point me to an open source project, where I can see, how it's really used? I think browsing the code and seeing the whole setup shows me more than the ususal snippets in the introduction articles you find around the web. Thanks in advance!
The Wave Protocol Server is my favourite example app.
I struggled a bit with this exact issue. It's so abstract and simple I was always worried I was "doing it wrong".
I've had been using it in the main project which has dependencies on other projects because the Guice module which sets the bindings was part of the main project.
I finally realized the libraries should be supplying the Modules themselves. At that point you can depend only on an instance of a Module (not a specific one), and the interfaces that are bound by it.
Taking it one step better, you can use the new ServiceLoader mechanism in Java 6 to automatically locate and install all Guice modules available on the classpath. Then you can swap in dependencies just by changing class path (db-real.jar vs. db-mock.jar).
I understand you're in Java-land, but in the .NET space the are several open-source apps written using an inversion of control container. Check out CodeCampServer, in which the UI module doesn't have a reference to the dependency resolution module. There is an HttpModule that does the work. (an HttpModule is just a external library you can plug in that handles events in ASP.NET, in CodeCampServer the UI project loads this DependencyRegistrarModule at run time, without any compile time reference to it.)
I think dependency injection has a way of disappearing from view if used properly, it will be just a way of initializing/wiring your application -- if it looks more fancy than that you are probably looking at extra features of the framework at hand, and not at the bare-bones dependency injection.
Edit: I'd recommend actually starting to use it instead of trying to find examples, and then come back and post questions here if you can't get stuff to work like you'd think it should :-)

Categories