views:

1527

answers:

2

How would I go about writing some code to allow access to a Java class in my webapp from the command line.

E.g. I have a java class with command line interface, that can runs code in the context of the webapp, with access to the DB etc. I want to log on the machine hosting my WARred app in tomcat and be able to interact with it

Where should i start looking ?

Thanks

A: 

A suggestion: Your command line interface class should accept an InputStream as it's input and provide an OutputStream (it can't hardcode output to System.out and input to System.in) that it's output will be written to. Then you'll have to write a server class that listens for connections on a certain port. When a connection is made the server would take the InputStream from the connection and give it to the command line class which would provide the OutputStream that data written to will be passed to the client that made the connection.

+3  A: 

Do you just want to run class files that just so happen to be bundled in the WAR, or do you want ot interact with the actual, running WAR instance? If the former, then the WAR is just a normal Jar file and you can execute classes in that just like any other other Jar file.

If you want to interact with the running WAR, then you might want to look at JMX.

All current JDKs (at least 1.5+) come with JMX "for free". It's easy to create little interface classes to be used as commands to interact with your WAR.

THen you would need to create a command line program that connects to the WAR via JMX, or you can use a tool like JConsole (which comes with the JDK, but it's a GUI) to interact with your instance. There are other JMX clients out there as well.

If none of that is attractive, there's always web services.

Will Hartung
Thanks thats what I was looking for. JMX is up to the job then. Will explore that. Thks
With regards to "WAR is just a normal Jar file and you can execute classes in that just like any other other Jar file", that implies you could say "java -cp foo.war ..." or even "java -jar foo.war" and have it work like a jar file. Can you *really* do that?
Licky Lindsay