This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Programatically Running a script file in DOS via Java Application

Jun 6th, 2011, 03:18 PM

Dear all,
am playing around with Spring Roo, specifically running java apps from their roo scripts. Normally this is done by going onto the command shell, navigating to the directory where the *.roo file is, and running the command

PHP Code:

C:\> roo script application.roo

This is all done manually. However now I want to run the roo script but from another java application. How can I I get the java application to run the roo script command automatically?

There's no Java API for invoking Roo, so your only option is to use java.lang.Runtime#exec() as you would for invoking any external program from Java. Note that because Roo runs from a batch file, you might need to invoke CMD.EXE with the /C argument instead of directly calling roo.bat (this is a batch file thing, not a Roo thing). So the call to exec would look like this:

Comment

Just because I'm curious, why are you running Roo from another Java application?

Well its a bit of a long story but here goes... Well after playing with Roo a couple of weeks ago, it struck me at how quickly one could get a usable data collection application (I work in a health research institute, so data collection is our bread and butter) working just by defining your entities, their attributes and their relationships to each other (as well as any validations, range checkers, etc with JSR303 bean validation) , and presto, you have a usable data capture tool. So I thought "how can I make this powerful functionality available to the average layman (our research scientists) so they can have data capture apps in minutes?"

I decided to build an application (Generic Application Generator) which lets a user define his entities, the attributes for these entities and the relationships between these entities, all from a roo generated user interface. once these entities are created, when these entities'

PHP Code:

persist()

method is called, it in turn calls on another method, generateRooScript() and a Roo script is created (i.e. translating the entity, its attributes and relationships into a Roo script and saving it somewhere in the file system eg C:\\Users\\Tumaini\\Desktop). This part is already done.

So now I want the Generic Application Generator to run this roo script so that the project files are created, and once this is done the Generic Application Creator can programatically go to the generated projects

PHP Code:

\src\main\webapp\images

folder and replace the roo default banner image "banner-graphic.png" with a bespoke banner.

Thus a bespoke data collection application can be created in record time, (under a day once the entities, attributes and relationships are well thought out).

Am still wondering how I can implement skip logic though, but so far its working well, the secondary app is being created from the roo script generated by the first app (albeit with the default Roo banner, and Roo welcome message on the home page... need to sort that out too, not sure how just yet).

If any of this does not make sense please feel free to ask, as am still quite new to Roo (and Spring in general) myself and will probably need more help along the way with the more complex requirements, but in terms of a prototype, I have a working prototype that works well and shows the idea is workable. Any criticisms also welcomed .

Thanks!

Comment

There's no Java API for invoking Roo, so your only option is to use java.lang.Runtime#exec() as you would for invoking any external program from Java. Note that because Roo runs from a batch file, you might need to invoke CMD.EXE with the /C argument instead of directly calling roo.bat (this is a batch file thing, not a Roo thing). So the call to exec would look like this:

Comment

By the way, I have to mention am running this from within Netbeans and not on the Windows command shell, not sure if that makes a difference?

It might; some IDEs (e.g. Eclipse) can ship with their own JREs. It's possible that your NetBeans installation is using its own 32-bit JRE, whereas when you run "java -version" from the command line, some other (64-bit) JRE is being called. You should be able to see (and probably configure) somewhere in NetBeans which JRE it uses; in Eclipse there's a configuration dialog that lets you add any number of JREs, then per project you can specify which one of those to use. Eclipse also prints out the full path to javaw.exe in the console whenever you execute a main method; maybe NetBeans does something similar. Even simply doing this will tell you which broad version of Java your program is actually using (although not the exact JRE type):

Comment

So should I take it Roo is working on supporting NOSQL databases in the future?

We're getting off-topic, but yes, we're working on ROO-301, which will allow you to use DAOs and repositories (that you generate via a custom add-on or write by hand) that use whatever persistence technology works for you. Later enhancements might add support for a specific data store, but nothing's on the road map at the moment.

In the meantime, please make sure your wishes are logged as JIRA issues where people can vote for them and comment on them.