gpt4 book ai didi

java - 在 JNA 中重定向输出并静默生成子进程(无弹出命令窗口)

转载 作者:行者123 更新时间:2023-12-01 14:03:54 25 4
gpt4 key购买 nike

我已经编写了一个 java 程序来生成子 java 进程。当它产生子进程时,它会打开一个命令提示符窗口,并将输出重定向到命令提示符。你能告诉我如何在没有额外命令窗口的情况下生成/创建子进程并将这个进程的输出重定向到一个文件。我正在使用 Java 6 和 JNA 3.5.2

下面是创建进程的命令

cmd = "c:\\jdk1.6.0_30\\bin\\java.exe -Xmx256m -Xss1024k  -cp <classpath jars>; 
-Dorg.omg.CORBA.ORBClass=org.jacorb.orb.ORB com.xyz.test.MainProgram <program arguments>

带有弹出命令提示符的工作代码

private boolean createChildProcess(ProcessData processData,String cmd,String appName){
HANDLEByReference childStdInRead = new HANDLEByReference();
HANDLEByReference childStdOutWrite = new HANDLEByReference();


    int STARTF_USESTDHANDLES = 0x00000100;

WinBase.PROCESS_INFORMATION.ByReference processInformation = new WinBase.PROCESS_INFORMATION.ByReference();
STARTUPINFO startupInfo = new STARTUPINFO();

// Create the child process.
if (!Kernel32.INSTANCE.CreateProcess(
null,
cmd,
null,
null,
true,
new DWORD(0x00000020), //new DWORD(0x00000001)
null,
null,
startupInfo,
processInformation)){
System.err.println(Kernel32Util.formatMessageFromLastErrorCode(Kernel32.INSTANCE.GetLastError()));
return false;

}else {
System.out.println("Created Process :"+processInformation.dwProcessId.intValue());
processData.setProcessId(processInformation.dwProcessId.intValue());
//processData.sethProcess(processInformation.hProcess);
//com.sun.jna.platform.win32.Kernel32.INSTANCE.WaitForSingleObject(processInformation.hProcess, 0xFFFFFFFF);
com.sun.jna.platform.win32.Kernel32.INSTANCE.CloseHandle(processInformation.hProcess);
com.sun.jna.platform.win32.Kernel32.INSTANCE.CloseHandle(processInformation.hThread);
return true;
}
} </pre>

我尝试了以下代码,但没有用


private boolean createChildProcess(ProcessData processData,String cmd,String appName){

//Tried with Output to file
HANDLEByReference childStdInRead = new HANDLEByReference();
HANDLEByReference childStdInWrite = new HANDLEByReference();
HANDLEByReference childStdOutRead = new HANDLEByReference();
HANDLEByReference childStdOutWrite = new HANDLEByReference();

int STARTF_USESTDHANDLES = 0x00000100;

WinBase.PROCESS_INFORMATION.ByReference processInformation = new WinBase.PROCESS_INFORMATION.ByReference();
STARTUPINFO startupInfo = new STARTUPINFO();
startupInfo.cb = new DWORD(processInformation.size());
startupInfo.hStdError = childStdOutWrite.getValue();
startupInfo.hStdOutput = childStdOutWrite.getValue();
startupInfo.hStdInput = childStdInRead.getValue();
startupInfo.dwFlags |= STARTF_USESTDHANDLES;

SECURITY_ATTRIBUTES saAttr = new SECURITY_ATTRIBUTES();
saAttr.dwLength = new DWORD(saAttr.size());
saAttr.bInheritHandle = true;
saAttr.lpSecurityDescriptor = null;

// Create a pipe for the child process's STDOUT.
if (!com.sun.jna.platform.win32.Kernel32.INSTANCE.CreatePipe(childStdOutRead, childStdOutWrite, saAttr, 0)){
System.err.println(Kernel32.INSTANCE.GetLastError());
}

// Ensure the read handle to the pipe for STDOUT is not inherited.
if (!com.sun.jna.platform.win32.Kernel32.INSTANCE.SetHandleInformation(childStdOutRead.getValue(), HANDLE_FLAG_INHERIT, 0)){
System.err.println(Kernel32.INSTANCE.GetLastError());;
}

// Create the child process.
if (!Kernel32.INSTANCE.CreateProcess(
null,
cmd,
null,
null,
true,
new DWORD(0x00000020), //new DWORD(0x00000001)
null,
null,
startupInfo,
processInformation)){
System.err.println(Kernel32Util.formatMessageFromLastErrorCode(Kernel32.INSTANCE.GetLastError()));
return false;

}else {
System.out.println("Created Process :"+processInformation.dwProcessId.intValue());
processData.setProcessId(processInformation.dwProcessId.intValue());
//processData.sethProcess(processInformation.hProcess);
//com.sun.jna.platform.win32.Kernel32.INSTANCE.WaitForSingleObject(processInformation.hProcess, 0xFFFFFFFF);

HANDLE inputFile = com.sun.jna.platform.win32.Kernel32.INSTANCE.CreateFile(
"c:\\"+processInformation.dwProcessId.intValue()+".txt",
GENERIC_READ,
0,
null,
OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY,
null);
//ReadFromPipe(childStdOutRead,childStdOutWrite);
com.sun.jna.platform.win32.Kernel32.INSTANCE.CloseHandle(processInformation.hProcess);
com.sun.jna.platform.win32.Kernel32.INSTANCE.CloseHandle(processInformation.hThread);
return true;
}
}

最佳答案

在 Windows 上,调用 javaw.exe而不是 java.exe .这将避免“控制台”窗口。

至于重定向到文件,最简单的方法可能是将参数传递给 Java 子进程以指示所需的文件,然后重定向 System.outSystem.err写入该文件。
System.outSystem.errPrintStream s,因此在启动时,您可以简单地创建自己的(映射到 FileOutputStream )以重定向所有控制台输出。见 this question on SO详情。

关于java - 在 JNA 中重定向输出并静默生成子进程(无弹出命令窗口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19094450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com