Announcement Announcement Module
Collapse
No announcement yet.
Programatically Running a script file in DOS via Java Application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Programatically Running a script file in DOS via Java Application

    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?

  • #2
    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:
    Code:
    String[] command = {"cmd", "/c", "roo.bat", "script", "application.roo"};
    Process process = Runtime.getRuntime().exec(command);
    If you want to capture Roo's output/error streams, or get its exit code, you can do so using the returned Process instance (again, none of this is Roo-specific).

    I haven't tried this out, so let us know how it goes.

    Just because I'm curious, why are you running Roo from another Java application?
    Last edited by Andrew Swan; Jun 6th, 2011, 06:42 PM. Reason: Added details about Process

    Comment


    • #3
      Originally posted by Andrew Swan View Post
      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


      • #4
        Originally posted by Andrew Swan View Post
        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:
        Code:
        String[] command = {"cmd", "/c", "roo.bat", "script", "application.roo"};
        Process process = Runtime.getRuntime().exec(command);
        I have written a mini program as follows to see how this works:

        PHP Code:
        package io;

        import java.io.*;

        public class 
        ExecuteDosCommand {

            public static 
        void main(String[] args) {
                try {
                    
        String[] command = {"cmd""/c""roo.bat""script",
                    
        "C:\\roo_scripts\\application.roo"};

                    
        Process p Runtime.getRuntime().exec(command);

                    
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                    
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                    
        // read the error output from the command

                    
        String s null;
                    
        System.out.println("Here is the standard error of the command (if any):\n");
                    while ((
        stdError.readLine()) != null) {
                        
        System.out.println(s);
                    }

                    
        // output from the command

                    
        System.out.println("Here is the output from the command");
                    while ((
        stdInput.readLine()) != null) {
                        
        System.out.println(s);
                    }
                    
        System.out.println("I am In try");
                } catch (
        Exception e) {
                    
        System.out.println("I am In catch");
                }
            }

        However, on running this programme, I get the following error:

        Code:
        run:
        Here is the standard error of the command (if any):
        
        Exception in thread "Spring Roo JLine Shell" java.lang.UnsatisfiedLinkError: C:\Users\Sir Tumaini Kilimba\AppData\Local\Temp\jline_.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
                at java.lang.ClassLoader$NativeLibrary.load(Native Method)
                at java.lang.ClassLoader.loadLibrary0(Unknown Source)
                at java.lang.ClassLoader.loadLibrary(Unknown Source)
                at java.lang.Runtime.load0(Unknown Source)
                at java.lang.System.load(Unknown Source)
                at jline.WindowsTerminal.loadLibrary(WindowsTerminal.java:322)
                at jline.WindowsTerminal.initializeTerminal(WindowsTerminal.java:240)
                at org.springframework.roo.shell.jline.JLineShell.createAnsiWindowsReader(JLineShell.java:173)
                at org.springframework.roo.shell.jline.JLineShell.run(JLineShell.java:73)
                at java.lang.Thread.run(Unknown Source)
        Does anyone know what may cause this? Am on a Windows 7, 64 Bit OS...
        Last edited by BlakkPhoenixx; Jun 7th, 2011, 03:31 AM.

        Comment


        • #5
          By any chance are you using an x86 (32-bit) JRE instead of an x64 (64-bit) JRE?

          Comment


          • #6
            Originally posted by Andrew Swan View Post
            By any chance are you using an x86 (32-bit) JRE instead of an x64 (64-bit) JRE?
            Hello Andrew. This is what am running on:

            Code:
            C:\Users\Sir Tumaini Kilimba\Generic Application\genapp>java -version
            java version "1.6.0_21"
            Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
            Java HotSpot(TM) 64-Bit Server VM (build 17.0-b17, mixed mode)
            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?

            Comment


            • #7
              Originally posted by BlakkPhoenixx View Post
              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):
              Code:
              System.out.println(System.getProperty("java.version"));
              If it's not 1.6.0_21, you know there's multiple JREs installed.

              Comment


              • #8
                @BlakkPhoenixx,

                There might be another way to accomplish your GaG app in a more orthodox way...
                Instead of using a RDBMS for it you can use a key-value one, in other words a NOSQL DB.

                A NOSQL db would give the flexibility of creating you entities on the fly.

                I believe that would give you a cleaner implementation all the way from the conceptualization.

                My 2 cents approach on it.

                Thx
                B. Roogards
                jD

                Comment


                • #9
                  Originally posted by delgad9 View Post
                  @BlakkPhoenixx,

                  There might be another way to accomplish your GaG app in a more orthodox way...
                  Instead of using a RDBMS for it you can use a key-value one, in other words a NOSQL DB.

                  A NOSQL db would give the flexibility of creating you entities on the fly.

                  I believe that would give you a cleaner implementation all the way from the conceptualization.

                  My 2 cents approach on it.

                  Thx
                  B. Roogards
                  jD
                  Yes, but where would I get all the wonderful web scaffolding from then? The dynamically generated interfaces?

                  Comment


                  • #10
                    @BlakkPhoenixx,

                    I see...
                    I just tossing you another way to do your thing.

                    Roogards
                    jD

                    Comment


                    • #11
                      Originally posted by BlakkPhoenixx View Post
                      Yes, but where would I get all the wonderful web scaffolding from then? The dynamically generated interfaces?
                      Working on it, working on it... ;-)

                      Comment


                      • #12
                        Thanks :-)

                        Comment


                        • #13
                          Originally posted by Andrew Swan View Post
                          Working on it, working on it... ;-)
                          So should I take it Roo is working on supporting NOSQL databases in the future?

                          Comment


                          • #14
                            Originally posted by BlakkPhoenixx View Post
                            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.

                            Comment

                            Working...
                            X