Newbie question - execute jar file in ruby
-
Hey!
I think it is a very basic question, but I can't solve it.
I would like to execute a jar file in Sketchups ruby.
My serveral experiments:
output =
Model2GCode #{ARGSTRING}
=> no such file found
output =Model2GCode.jar #{ARGSTRING}
=> no such file found
output =Model2GCode -jar #{ARGSTRING}
=> no such file foundI think I have to give them the right folder. (the jar file is in the same folder as the rb file).
Do you have any ideas?
Thank you very much for your help!
-
oldDir = Dir.getwd Dir.chdir('C;/some/path/to/the/correct/dir') output = nil output = `Model2GCode.jar #{ARGSTRING}` delay = 0.5 tid = UI.start_timer( delay, true ) { if output UI.stop_timer(tid) Dir.chdir(oldDir) end }
-
@dan rathbun said:
oldDir = Dir.getwd > Dir.chdir('C;/some/path/to/the/correct/dir') > output = nil > output = `Model2GCode.jar #{ARGSTRING}` > delay = 0.5 > tid = UI.start_timer( delay, true ) { > if output > UI.stop_timer(tid) > Dir.chdir(oldDir) > end > } >
Oh thank you very much. Now, I just get an error like this: "Error: #<Errno::ENOEXEC: Exec format error - Model2GCode.jar", but I think that's a problem of my jar file!
Thanks a lot for your help!
-
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 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!
-
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... -
@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}`` -
That's almost it... but it needs a
-jar
flag to execute the .jar file...
UseDir.chdir()
to the jar file's folder folder then use
output =
java -jar Model2GCode.jar #{ARGSTRING}``
[Works for me...] -
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
-
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!
-
$?
isnil
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 eitherNilClass
norInteger
(nor it's subclasses, such asFixnum
.)You should test the
output
reference (since you first set it tonil
, which Ruby will eval asfalse
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 theThread
class (and raises aNoMethodError
exception.)Use the global method
sleep()
(which is inherited from moduleKernel
,) instead.
Warning.. usingsleep()
can cause "whiteout" (Windows can put the SketchUp application into "ghost-window mode".) -
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 aSystem.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?!
-
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 ??
-
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 -
@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.
-
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
Advertisement