Call from java Main class from servlet Application - java

I Have two applications.One is Standalone application.Another one is web Application (Servlet App).
Here I want to Call Java Application From Servlet Application.So can You Guys Suggest Me or Can i have one app for this one.So that i will implement in my application.
In this Application I have created Jar file of Stand alone Application After that What to do ?

First, you should make your command line application classes available in your web applcaction environment. To achieve this you should either:
package all in one jar
put classes of stand alone application under WEB-INF/classes in you war
put jar that contains your stand alone application under WEB-INF/lib in you war
Stand alone application starts from main() method. You can just call this method from your servlet and pass parameters, for example: MyApp.main("hello", "stand alone application").
Since you are the author of both applications and are familiar with the internal design of the stand alone application you could (and probably should) call internal layer directly without using main(). For example if you main method starts with new MyApp().start() perform the same call from your servlet.

Just Switch from GlassFish to Tomcat , And then your problem has been solved.

Related

Call java application launched from another container

Here's situation. I have one container with php code and php-fpm - this is my application container. Sometimes main application calls java application - jar file. So I decided to split those technologies and make seperate java container with this java application. Now I need a way to call jar file launched inside another container. One way is rebuilding java applicaton to support REST api, but it takes time so is there any other possibility to solve this problem?
You could take a look at Java Remote Method Invocation (Java RMI), but to be honest, I have no idea if it's possible to to this invocation not using java.
https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/hello/hello-world.html

working as servlet but not as java class. why?

I am working on SAML SSO Authentication.
I have created a servlet to generate SAML metadata and i deployed it and run it and I got the output.
Same time I have created a java class to generate SAML Metadata with the same code and tried to run it independently. I have added the same Jar files that I have used for that servlet application.
But I got the Exception given below. Can anybody help me to find the difference between running an application independently and by using java servlet??
Thanks in advance.
Exception:
Running as servlet in a web container means all sorts of stuff is on the classpath that is automatically provided by the servlet container.
Running using main() means you have to put all needed stuff on the classpath yourself. The ClassNotFoundException you got should be clear enough in that respect.
(Pls note that although I did say "the" classpath, in a servlet container things are typically not quite that simple. But that's not the point. Also note that running as a servlet, and using features of libraries provided for the container, may even mean your stuff cannot run as an independent java program simply because the library stuff was deliberately intended for servlet container use exclusively.)

Run main method in EJB on Deployment of EAR

I have created one java project which has one Main class. So I am running this Java Project using this Main class main method . But now my requirement is changed that I have to create the EJB of this project, and deployable artifact will be EAR. So still in the search of that how i will run this main method on the deployment of this EAR in Websphere.
Just want to summarize that I want to execute main method in EJB on the deployment of EAR in WebSphere, searched a lot try one or two methods but still searching for some good way.
From your question and comments it is not entirely clear to me if you fully understand how EJBs work (i.e. their lifecycle, how they get called, etc.). If you haven't done so, I recommend reading a Java EE tutorial, e.g. this one from Oracle.
Once you have an overview how things work, have a look here and choose the way which fits best for your case.
Common to all ways is that you cannot have a main method as before, but you have to convert it to a normal public method of your EJB which will get called at startup.
Since you are using and EAR anyway, you can also package a WAR inside the EAR along your EJB, and then use the ContextListener method described in my link.
But also nowadays there is almost no need to have an EAR. You could package everything in a WAR. See here: Why use an ear instead of a war?
As far as I know, if you're on Java EE 6 and you want your EJBs methods to be called in a scheduled manner, you can make a scheduler EJB by annotating it with #Schedule, inject your EJBs in it and make the scheduled method to call your injected EJBs methods. If that's not the case and you want to call them on demand, you need a client like a Servlet in which you inject them.

Groovy cast exception occurring in deployed web application after script modification

I asked a similar question previously, but wanted some clarification on the mechanics of the GroovyScriptEngine and how class loading is performed. I have a Vaadin web application that contains groovy classes in WEB-INF. The webapp loads UI logic via a GroovyScriptEngine. Here is a sequence of events that leads to an error:
Deploy war to tomcat & start server, application runs as expected
I make an insignificant change to groovy file located in the exploded WEB-INF folder (for instance, a remark)
Refresh page, GSE apparently reloads classes, and application bombs with GroovyCastException: Cannot cast object 'com.company.myclass#7cde31f8' with class 'com.company.myclass' to class 'com.company.myclass'.
I understand that this may be a class loader issue. Suggestions? I thought the whole point of the GroovyScriptEngine was that it handled hot-edited groovy classes without having to restart the container.
For sure this is ClassLoader issue.
If you need to reload classes (I suppose for faster development), try JRebel. Works well with groovy and tomcat.

Is it possible to include/embed one Java EE application(war file) inside another?

I have an application which is a portal application and I want to allow other users add their applications to it. In order to do this I need some way to be able to access their applications in mine. Is this possible?
You cannot put WARs inside of other WARs. You need an EAR file to contain WARs, EJBs, etc. One way to implement inter-WAR communication is to package that logic directly in the EAR. It all depends on what you're trying to do.
the way to do inter .WAR communication is by the method
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getContext(java.lang.String)
ServletContext.getContext(URIOfOtherWAR_resource)
I've used this succesfully for doing what you're talking about.
Maybe you need a plugin system or portlet, so your user will not develop a war application but include their portlet inside your application (war). There's a standard : JSR 168 and several implementations :
http://developers.sun.com/portalserver/reference/techart/jsr168/
As others have pointed out, embedding WARs inside WARs is not an option. However, I may have a workaround for you.
Most Web containers I'm familiar with have a "test deployment / auto deploy" mode / capability, where they will automatically deploy an application if the WAR is copied into the right directory.
Your portal application could certainly allow uploading WARs, and it could store the uploaded bytes in a given directory under a given file name. Your Web container could do the rest. You could then link to the new application from your portal, or whatever. All this is relatively easy to do.
However, be advised that this is a horrible idea if there is any security concern whatsoever. You are essentially allowing your users to execute arbitrary code on your server. Unless you completely trust all potential users to be both non-malicious and perfectly competent (think infinite loops), you are asking for a lot of trouble here.

Categories

Resources