sketchucation logo sketchucation
    • Login
    ⚠️ Attention | Having issues with Sketchucation Tools 5? Report Here

    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.
    • 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