1

GlassFish Application Server uses a script, asadmin.bat, that in turns starts a JVM.

I'd like to call this script using jinterop and DCOM from Java on a remote machine. I can't find any help on this specific usage. Any help would be greatly appreciated.

codeplumber
  • 469
  • 2
  • 11
  • Björn -- You are my new hero! It all worked perfectly. Thanks so much. I don't have enough points to give you points yet - regrettably... – codeplumber Sep 20 '11 at 00:15

1 Answers1

4

I use the Windows Scripting Host Shell to execute some program or batch on a remote computer.

The code looks like:

// Create a session
JISession session = JISession.createSession(<domain>, <user>, <password>);
session.useSessionSecurity(true);

// Execute command
JIComServer comStub = new JIComServer(JIProgId.valueOf("WScript.Shell"),<IP>, session);
IJIComObject unknown = comStub.createInstance();
final IJIDispatch shell =     (IJIDispatch)JIObjectFactory.narrowObject((IJIComObject)unknown.queryInterface(IJIDispatch.I ID));
JIVariant results[] = shell.callMethodA("Exec", new Object[]{new JIString("%comspec% /c asadmin.bat" )});

If you need the output from the batch you can use StdOut to read it.

JIVariant stdOutJIVariant = wbemObjectSet_dispatch.get("StdOut"); 
IJIDispatch stdOut =  (IJIDispatch)JIObjectFactory.narrowObject(stdOutJIVariant.getObjectAsComObject());

// Read all from stdOut
while(!((JIVariant)stdOut.get("AtEndOfStream")).getObjectAsBoolean()){ 
    System.out.println(stdOut.callMethodA("ReadAll").getObjectAsString().getString()); 
} 
Shyam Bhimani
  • 1,310
  • 1
  • 22
  • 37
Björn
  • 151
  • 3
  • It worked great after I solved multiple layers of Firewalls. But where does this variable come from: wbemObjectSet_dispatch – codeplumber Sep 06 '11 at 03:02
  • ok i forget the line 'final IJIDispatch wbemObjectSet_dispatch = (IJIDispatch)JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());' so it came from the results array. – Björn Sep 11 '11 at 11:39
  • I'm getting an error while running this, do I have to enable "Use Surrogate Process" flag? – hanish.kh Mar 20 '14 at 07:14