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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling a method from a separte application?" Watch "Calling a method from a separte application?" New topic
Author

Calling a method from a separte application?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys! Is this possible? I have two applications. One is a webapp that runs in tomcat and uses tomcat's connection pooling for database access and a standalone application which is run in the command line. I would like to know if it's possible to call my database Class's methods through a separate application? For example, I have a list of Employees in the standalone app and I need to populate each by calling a method in the tomcat application.

ArrayList<Employees> eList = //call from tomcat app that returns an Employee List or ArrayList

Is this possible? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
While it's possible to call into another application by a number of means (sockets, RMI, web services, ...), can you clarify why the method needs to run inside of the web app? Can't the relevant classes be part of the CLI application as well? Or do you need access to runtime objects of the web app?


Ping & DNS - updated with new look and Ping home screen widget
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
While it's possible to call into another application by a number of means (sockets, RMI, web services, ...), can you clarify why the method needs to run inside of the web app? Can't the relevant classes be part of the CLI application as well? Or do you need access to runtime objects of the web app?



Hi, I think what I need are runtime Connection object of the application. Right now, when I try to run the app I get this error.


E:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\zstats\WEB-INF\cl
asses>java dk/zmag/http/process/DailyUpdate
Starting daily backup...
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at dk.zmag.http.dao.ConnectionPoolManager.getConnectionFromPool(Connecti
onPoolManager.java:43)
at dk.zmag.http.dao.DAOEssentials.acquireConnection(DAOEssentials.java:1
9)
at dk.zmag.http.dao.mysql.MySQLMagazineDAO.selectAllMagazineFromDB(MySQL
MagazineDAO.java:251)
at dk.zmag.http.process.DailyUpdate.makeUpdate(DailyUpdate.java:45)
at dk.zmag.http.process.DailyUpdate.main(DailyUpdate.java:95)
java.lang.NullPointerException
at dk.zmag.http.dao.mysql.MySQLMagazineDAO.selectAllMagazineFromDB(MySQL
MagazineDAO.java:255)
at dk.zmag.http.process.DailyUpdate.makeUpdate(DailyUpdate.java:45)
at dk.zmag.http.process.DailyUpdate.main(DailyUpdate.java:95)


Connection pooling works ok in my webapp but not in my standalone app. I didn't want to rewrite the database methods that I already created that's why I chose to put the standalone app inside the webapp itself. The standalone app will be called using a cron scheduler to make daily backups of the database.
Ådne Brunborg
Ranch Hand

Joined: Aug 05, 2005
Posts: 208
Let me see if I understand the situation.

1) You have a webapp in Tomcat, which are doing CRUD-stuff on a database.

2) You (want to) have an application doing other stuff on the database too.

In order to achieve 2), you want to reuse the stuff you did in 1) as much as possible. Sensible.

I didn't want to rewrite the database methods that I already created that's why I chose to put the standalone app inside the webapp itself.


Have you considered moving your database methods outside the webapp instead of moving your standalone app inside the webapp? Make a separate project and .jar-file which you call from either of your apps?
[ December 19, 2006: Message edited by: �dne Brunborg ]

Entia non sunt multiplicanda praeter necessitatem
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13872
    
  10

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

Tomcat provides a naming service that you can access via JNDI from your web application. You probably declare the Tomcat connection pool in a Tomcat XML configuration file (server.xml). In your web application, you're looking up the database connection pool that Tomcat has put in the naming directory for you.

When you run a stand-alone application (outside of Tomcat), you don't have access to Tomcat's naming service directly. So when you try to lookup something via JNDI, it will not be found - JNDI isn't even configured (that's what the error message says).

Ofcourse you can access the database from a stand-alone application, but you can't use Tomcat's connection pool easily; you need to make sure that you connect to Tomcat's naming service to do this. It might be easier to just make a directy JDBC connection to the database from your stand-alone tool. The stand-alone tool is a tool to backup the database, isn't it? It doesn't really need to use a connection pool anyway. (One of the reasons to use a connection pool in a web app is to control the number of connections to the database, regardless of how many users are using the web app - your backup tool just needs 1 connection).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18113
    
  39

I only know this in passing -- have not using JNDI in Tomcat much, so take this with a grain of salt.

I believe there is a way to turn on remote JNDI service in Tomcat. Something with activating / running some kind of service in Tomcat, that will sit on a fix known port, and route requests to the internal JNDI service.

I thought that I saw examples of this, with either the Apache or JBoss documentations... not much help, but maybe this is a small start.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys! I just made a workaround where the standalone app makes the usual way of getting a connection (if connection pooling fails, i.e. it will). Makes my life easier and I still use the database classes from the web app.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a method from a separte application?
 
Similar Threads
UnsatisfiedLinkError in JBoss while calling a JNI method
Problem in usebean
Authentication for different user type
Updatable DataGrid: JSP
spring shut down hook