File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes When tomcat starts... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "When tomcat starts... " Watch "When tomcat starts... " New topic
Author

When tomcat starts...

Aditya Raghu
Greenhorn

Joined: Aug 11, 2009
Posts: 11
Hello all,

I need to run a script as soon as tomcat starts each time (In Linux). Can you please suggest a way in which this can be done?

Thanks
Aditya



Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Aditya Raghu wrote:I need to run a script as soon as tomcat starts each time (In Linux).

Whats the script do ? Is it needed for running a Java web app ? If yes then I suggest try implementing ContextListener for that web app. If not, then we've to wait for experts comment

And welcome to JR.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Aditya Raghu
Greenhorn

Joined: Aug 11, 2009
Posts: 11
Thanks a lot for your reply sagar . Well, I want to run a java jar which is a swing applet.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
You need to study java.lang.Runtime -or- ProcessBuilder - which allow you to execute separate processes, such as executing a script.

As Sagar said, a ContextListener would be one place to execute your script.

Carefully read the Process docs with respect to the requirement for handling standard IO streams.

Bill
Aditya Raghu
Greenhorn

Joined: Aug 11, 2009
Posts: 11
I want to run a swing application jar. I can run the application from an ordinary '.sh' file, but I'm not able to run the jar from startup.sh. I used this command at the end of the startup.sh file- 'java -jar frontend2.jar'. when i run startup.sh it starts tomcat properly but does nothing with the jar. Is this the right way to do this? I even tried calling another shell file from startup.sh, where I was running the jar but, even that did not work.

Please guide me on this...
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Aditya Raghu wrote: Well, I want to run a java jar which is a swing applet.

Well applet can be run by embedding them into JSP/HTML file, not by executing them within the Tomcat startup script, which is unusual thing to try !!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

Sagar Rohankar wrote:Well applet can be run by embedding them into JSP/HTML file, not by executing them within the Tomcat startup script, which is unusual thing to try !!!


"Unusual"... yes, that's the polite way to say it. I can't imagine any reasonable design which would require running an applet on the server at start-up time, or at any other time for that matter. I expect there has been some misunderstanding and this is not the actual requirement.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Paul Clapham wrote:
Sagar Rohankar wrote:Well applet can be run by embedding them into JSP/HTML file, not by executing them within the Tomcat startup script, which is unusual thing to try !!!

I expect there has been some misunderstanding and this is not the actual requirement.

I don't misunderstood anything, the OP has the requirement,

Aditya Raghu wrote:I want to run a swing application jar. I can run the application from an ordinary '.sh' file, but I'm not able to run the jar from startup.sh. I used this command at the end of the startup.sh file- 'java -jar frontend2.jar'. when i run startup.sh it starts tomcat properly but does nothing with the jar. Is this the right way to do this? I even tried calling another shell file from startup.sh, where I was running the jar but, even that did not work.

and which is, as you said, no matter reasonable or correct.
Aditya Raghu
Greenhorn

Joined: Aug 11, 2009
Posts: 11
Oh ok I am very new to web development, but please bear with me . Here is my problem statement- I have a web application war in my web apps. I have to get tomcat run the war only when a proper license is submitted( licensing mechanism is not built yet). I have been asked to run the application from the present in the webapps folder based on the above condition. Is it possible in tomcat to configure and run an application selectively because here all other wars in the webapps folder should be left untouched.

Thanks
Aditya
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I have a web application war in my web apps.


If this is really a web application accessed by a HTTP request through Tomcat and generating a HTTP response, then normal web application security should apply.

Swing applications are "desktop" applications with a graphic user interface, not accessed by an HTTP request and not hosted by Tomcat but by the native operating system. If you contrive to start a Swing application on the server, only the server desktop interface can interact with it.

So - is the application a web application with HTTP or a desktop application?

Now, it is possible for Tomcat to control downloading of a desktop application - see the Java JNLP and Web Start forum.

Bill
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Aditya Raghu wrote:I have to get tomcat run the war only when a proper license is submitted( licensing mechanism is not built yet).

No, you shouldn't supposed play with tomcat if you can fulfil you needs simply by putting some licensing logic in web app.

Here are my 2 quick solutions:

  • 1. Deploy and check for the licence:
    Deploy the war, and show the "Upload your license" page, once user has uploaded it and you verify it, allow him to access the other protected pages, which need valid licence.


  • 2. Don't deploy war at all:
    Implement the context listener, which will check for the .lic file which is nothing but the encrypted license file. Once verified deploy the war otherwise exit


  • Now this two things require a good knowledge of Java web application working.. and about other method, lets fellow Ranchers provide their inputs
    Aditya Raghu
    Greenhorn

    Joined: Aug 11, 2009
    Posts: 11
    # 2. Don't deploy war at all:
    Implement the context listener, which will check for the .lic file which is nothing but the encrypted license file. Once verified deploy the war otherwise exit


    Thanks a lot for that Sagar. If you can, can you please briefly tell me how that can be done? I searched a lot for it but found very little on how a war can be exited in the servletcontextlistener implementation.

    Thanks,
    Aditya
    Sagar Rohankar
    Ranch Hand

    Joined: Feb 19, 2008
    Posts: 2902
        
        1

    Google ServletContextListener example
    In contextInitialized () method,
    Tim Holloway
    Saloon Keeper

    Joined: Jun 25, 2001
    Posts: 16145
        
      21

    You don't want to use Swing to natter about WAR licensing. On my production servers, that will fail because I don't run them with a GUI. Which gives me about and extra 100MB of RAM for webapps.

    In most cases, I'd go ahead and let the webapp run in demo mode, but block the ability for it to do serious work. It's a very effective marketing strategy. If your app looks good enough, people are more willing to pay to unlock it if they can see it running.


    Customer surveys are for companies who didn't pay proper attention to begin with.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: When tomcat starts...