Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes How to capture events from vbs script in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to capture events from vbs script in java " Watch "How to capture events from vbs script in java " New topic
Author

How to capture events from vbs script in java

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
How to capture events from vbs script in java

Hello all

i have vbs script that executing some actions after its done running or after its fails the vbs script

returns string , my question is how can i execute this script but also capture in java the returned string from the vbs script

Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
How would you launch your script from the command line? Just type the name? If so, you might be able to start it with ProcessBuilder and a command like

"rundll32 url.dll,FileProtocolHandler myScript.vbs"

Capturing output of the script is tougher. If you can make the script write the result to the console, you can capture that stdout in Java. This gets a bit tricky with threads and all. See if you can get the launch working and show us what you have.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
hello and thanks for the fast reply
what i meant is not to read the output from the cmd but is there is some kind of
way to receive the actual return parameter from vbs some way

some kind of vb wrapper or something like that ...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's what I tried:

I don't know why that process doesn't get the return code. But this one does:

Any of that help?
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
Hello and thanks for the fast reply

well indid i learned something here , but i like to get more deeper into that .

ok now i know if the script is terminated or not , but is there some kind of way to communicate with the running script

what i meant is : is there any way to get callbacks from the vbs script in runtime , and catch them in the java code ?

maybe this is more vbs question ... maybe there is some special object i need to use or something ..
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
for example say i have vbscript that do only that :

myVbs.vbs :



and i like to capture this "Hello World!" string in my java code how can it be done?

this is my java code , but its does not working ..

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by Stan James:
Here's what I tried:

I don't know why that process doesn't get the return code.


Wouldn't this give you the return value of the rundll32 program rather than the script ?


Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by Meir Yan:
for example say i have vbscript that do only that :

myVbs.vbs :



and i like to capture this "Hello World!" string in my java code how can it be done?

this is my java code , but its does not working ..



The Process output stream is the stream you write to to pass data to the process. If you want output from the process use the getInputStream/getErrorStream methods.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Wouldn't this give you the return value of the rundll32 program rather than the script ?


Sounds right to me.

But now we're back where I started before that little side trip - reading the output streams of the process. HERE is a story we often reference for exec problems. It includes a StreamGobbler class to read the output streams on separate threads, which is critical for some programs.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
as you can see from my code im allready doing it but it is not working
and i dont understand why ...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Originally posted by Meir Yan:
as you can see from my code im allready doing it but it is not working
and i dont understand why ...


No you're not. You're calling getOutputStream, which gives you a stream you can use to write to the process (provide input to the process you have started). If you want to get the output from the process you started, you need to call the getInputStream and/or getErrorStream methods and then read from the streams that they return.
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
hello all and thanks for the fast reply ..

now i think i understand and i have java code that will do the job , i have another question but im not sure its java related

when i run the vbs screen its printing the msg as popup and this is the the input i like to be the captured in to the java class

but without success does any one know how can i return value in vbs so the class could captors it ?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
WScript.Echo "Hello, World!" ??
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
nop i did it , not working its popup window ...
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
With the file test.vbs

At a command window, if I enter "wscript test.vbs" I get a pop-up window. If I enter "cscript test.vbs" I get console output. Try "exec cscript ..." instead of the rundll approach.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to capture events from vbs script in java