sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Newbie question - execute jar file in ruby

    Scheduled Pinned Locked Moved Developers' Forum
    16 Posts 3 Posters 3.3k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • TIGT Offline
      TIG Moderator
      last edited by

      As long as you are not passing any arguments to the jar file, then
      Dir.chdir('C:/some/path/to/the/correct/dir') UI.openURL('Model2GCode.jar')
      or perhaps if you pass the FULL path
      UI.openURL('file:/'+path2jar')
      will work...

      TIG

      1 Reply Last reply Reply Quote 0
      • N Offline
        niccah
        last edited by

        @tig said:

        As long as you are not passing any arguments to the jar file, then
        Dir.chdir('C:/some/path/to/the/correct/dir') UI.openURL('Model2GCode.jar')... UI.openURL('file:/'+path2jar')
        ...

        Unfortunately not - I want to send some information to the jar file. But UI.openURL() is a nice function too - I will keep it in my mind! Thank you!

        I have a further question:

        This java file can take serveral minutes to execute. So it would be nice, if I can get some signals from the java code to be sure, everything is okay.

        Does the following idea work?

        • after each step in the Java code, it should be send a short message to ruby
        • I can be sure, that every step of the java code should not be longer than 10 seconds
        • if I get no new messages after 10 seconds, I will cancle the ruby code

        If you think, it is an good idea to do this in such a way, than I don't know exactly, how I can "control" the messages of the Java code.

        If I write: output = Model2GCode.jar #{ARGSTRING}
        and the Java code send every seconds a message - gets the output "longer" and "longer"? Or will the Java code "overwrite" the "old" output?

        I hope, you know what I mean? It's hard for me to write what I am thinking πŸ˜„

        Thank you very much for your help!

        1 Reply Last reply Reply Quote 0
        • TIGT Offline
          TIG Moderator
          last edited by

          I use the UI.openURL() method with 'jar' files... BUT they are written to read their arguments from an 'ini' file written by the Ruby side. That way I can adjust what is read in, and not get tangled in passing data directly...

          If you have to wait until it's done processing... You could have the jar code simply delete its 'ini' file when it's finished doing its main stuff, and use something like
          sleep(0.2);wait=0.0 while File.exist?(inipath) sleep(0.2) wait+=0.2 break if wait >= 60.0 end#while
          where 'inipath' is the path read by the jar and you set wait's 'time-out' to whatever is sensible - here it's 1 minute - this is in case the jar fails and the 'ini' file never gets deleted...

          TIG

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            @niccah said:

            Oh thank you very much. Now, I just get an error like this: "Error: #<Errno::ENOEXEC: Exec format error - Model2GCode.jar",

            try:
            output =java Model2GCode.jar #{ARGSTRING}``

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              That's almost it... but it needs a -jar flag to execute the .jar file...
              Use Dir.chdir() to the jar file's folder folder then use
              output =java -jar Model2GCode.jar #{ARGSTRING}``
              [Works for me...]

              TIG

              1 Reply Last reply Reply Quote 0
              • Dan RathbunD Offline
                Dan Rathbun
                last edited by

                FYI the switches for java.exe (probably also javaw.exe)

                Usage; java [-options] class [args...]
                           (to execute a class)
                   or  java [-options] -jar jarfile [args...]
                           (to execute a jar file)
                
                where options include;
                    -client       to select the "client" VM
                    -server       to select the "server" VM
                    -hotspot      is a synonym for the "client" VM  [deprecated]
                                  The default VM is client.
                
                    -cp <class search path of directories and zip/jar files>
                    -classpath <class search path of directories and zip/jar files>
                                  A ; separated list of directories, JAR archives,
                                  and ZIP archives to search for class files.
                    -D<name>=<value>
                                  set a system property
                    -verbose[;class|gc|jni]
                                  enable verbose output
                    -version      print product version and exit
                    -version;<value>
                                  require the specified version to run
                    -showversion  print product version and continue
                    -jre-restrict-search | -jre-no-restrict-search
                                  include/exclude user private JREs in the version search
                    -? -help      print this help message
                    -X            print help on non-standard options
                    -ea[;<packagename>...|;<classname>]
                    -enableassertions[;<packagename>...|;<classname>]
                                  enable assertions
                    -da[;<packagename>...|;<classname>]
                    -disableassertions[;<packagename>...|;<classname>]
                                  disable assertions
                    -esa | -enablesystemassertions
                                  enable system assertions
                    -dsa | -disablesystemassertions
                                  disable system assertions
                    -agentlib;<libname>[=<options>]
                                  load native agent library <libname>, e.g. -agentlib;hprof
                                    see also, -agentlib;jdwp=help and -agentlib;hprof=help
                    -agentpath;<pathname>[=<options>]
                                  load native agent library by full pathname
                    -javaagent;<jarpath>[=<options>]
                                  load Java programming language agent, see java.lang.instrument
                
                    -splash;<imagepath>
                                  show splash screen with specified image
                

                πŸ’­

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • N Offline
                  niccah
                  last edited by

                  You are the best!

                  With the help of your tips I could solve the "Exec format error"! Thanks your very very much!

                  The next problem: πŸ˜†

                  I wrote two very easy jar files:

                  1th version: just: System.out.println("Hello world!"); => but with the following code, I get a false.

                  
                  output = nil
                  output = `java -jar Model2GCode.jar #{ARGSTRING}`
                  result=$?.success?
                  
                  

                  2nd version: Thread.sleep(10000); => I get a false too.

                  When I start the jar file outside of sketchup, everything is fine.

                  Do you have an idea, what can I do? I'm not sure, if it is an problem of Sketchup, Ruby or Java...

                  Thanks you for all your help!

                  1 Reply Last reply Reply Quote 0
                  • Dan RathbunD Offline
                    Dan Rathbun
                    last edited by

                    $? is nil when Sketchup first starts.
                    After a subprocess runs it may be set to a integer value, for the exit status.

                    The method name success? is not an instance method of either NilClass nor Integer (nor it's subclasses, such as Fixnum.)

                    You should test the output reference (since you first set it to nil, which Ruby will eval as false in a conditional statement.)

                    output = nil
                    output = `java -jar Model2GCode.jar #{ARGSTRING}`
                    if not output
                      if $? != nil || $? != 0
                        puts("The error code returned was; #{$?.to_s}")
                      else
                        puts("No error code was returned.")
                      end
                    else
                      # do something with the output string
                    end
                    
                    

                    You cannot call Thread.sleep() as it is a private instance method of the Thread class (and raises a NoMethodError exception.)

                    Use the global method sleep() (which is inherited from module Kernel,) instead.
                    Warning.. using sleep() can cause "whiteout" (Windows can put the SketchUp application into "ghost-window mode".)

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • N Offline
                      niccah
                      last edited by

                      Thanks Dan Rathbun!!

                      By using your method - I get an empty output.

                      
                      output = nil
                      output = `java -jar Model2GCode.jar #{ARGSTRING}`
                      if not output
                        if $? != nil || $? != 0
                          puts("The error code returned was; #{$?.to_s}")
                        else
                          puts("No error code was returned.")
                        end
                      else
                        UI.messagebox output
                      end
                      
                      

                      I see the UI.messagebox, but it is empty.
                      My Java code is just a

                      System.out.println("Hello world!");
                      

                      As I read in some forums, the "output" of the java application should be the "System.out.println"'s. So, I'm confused, why it doesn't work?!

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        Yes I tried it also, and got no output.
                        I tried in in a command shell and got java errors.

                        Sorry I cannot help you with java.

                        Why use java when SketchUp uses Ruby ??

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          Java won't print to the Ruby/SUp messagebox how does it know it.
                          Use an Alert in the Java code...
                          http://www.java2s.com/Code/JavaAPI/javax.microedition.lcdui/Alert.htm

                          TIG

                          1 Reply Last reply Reply Quote 0
                          • N Offline
                            niccah
                            last edited by

                            @dan rathbun said:

                            Yes I tried it also, and got no output.
                            I tried in in a command shell and got java errors.

                            Sorry I cannot help you with java.

                            Why use java when SketchUp uses Ruby ??

                            I'm coding a plugin for Sketchup to get a GCode for a CNC milling maschine for a 3D Model. So, multithreading would be very nice to accelerate the calculating. The ruby version of Sketchup doesn't support something like that. So I try to use an external Java application for the main calculations.

                            1 Reply Last reply Reply Quote 0
                            • Dan RathbunD Offline
                              Dan Rathbun
                              last edited by

                              Hmmm... OK. Yes, actually it's Ruby 1.8.x itself that uses "green threads" instead of native threads.

                              You might have more luck using a OLE interface to the java.

                              See: [Plugin Library] Win32API and Win32OLE so files
                              and: WIN32OLE rdoc

                              I'm not here much anymore.

                              1 Reply Last reply Reply Quote 0
                              • 1 / 1
                              • First post
                                Last post
                              Buy SketchPlus
                              Buy SUbD
                              Buy WrapR
                              Buy eBook
                              Buy Modelur
                              Buy Vertex Tools
                              Buy SketchCuisine
                              Buy FormFonts

                              Advertisement