简体   繁体   English

如何使用 jinterop 调用远程 bat 文件

[英]How to call a remote bat file using jinterop

GlassFish Application Server uses a script, asadmin.bat, that in turns starts a JVM. GlassFish Application Server 使用脚本 asadmin.bat 依次启动 JVM。

I'd like to call this script using jinterop and DCOM from Java on a remote machine.我想在远程机器上使用来自 Java 的 jinterop 和 DCOM 调用这个脚本。 I can't find any help on this specific usage.我找不到有关此特定用法的任何帮助。 Any help would be greatly appreciated.任何帮助将不胜感激。

I use the Windows Scripting Host Shell to execute some program or batch on a remote computer.我使用 Windows 脚本主机 Shell 在远程计算机上执行某些程序或批处理。

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.如果您需要批量中的 output,您可以使用 StdOut 读取它。

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()); 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM