我有一个类,它包含一个静态方法,用于使用JNA(Java NAtive Access)中的CreateProcess
api执行外部程序。
PS:我正在使用此代码找到here。
我的麻烦在于,当将ProcessInformation
类和StartupInfoA
作为静态类用于上述方法时,这会强制@Override
类型:
@Override
protected List getFieldOrder() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
我从来没有成功执行我的项目:-(。见:
所以,我问你: - 为它存在一些解决方案吗?
以下是我为实现目标而制作所有流程的方法:
package myProgram;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.WString;
import static com.sun.jna.platform.win32.WinBase.STARTF_USESHOWWINDOW;
import static com.sun.jna.platform.win32.WinUser.SW_SHOWNORMAL;
import com.sun.jna.win32.StdCallLibrary;
import java.util.List;
public class Execute {
public interface Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
boolean CreateProcessA(
String lpApplicationName
, String lpCommandLine
, Structure lpProcessAttributes
, Structure lpThreadAttributes
, boolean bInheritHandles
, int dwCreationFlags
, Structure lpEnvironment
, String lpCurrentDirectory
, Structure lpStartupInfo
, Structure lpProcessInformation);
}
public static class ProcessInformation extends Structure {
public Pointer hProcess;
public Pointer hThread;
public int dwProcessId;
public int dwThreadId;
@Override
protected List getFieldOrder() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
public static class StartupInfoA extends Structure {
public int cb;
public WString lpReserved;
public WString lpDesktop;
public WString lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public Pointer lpReserved2;
public Pointer hStdInput;
public Pointer hStdOutput;
public Pointer hStdError;
@Override
protected List getFieldOrder() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
public static void ExecuteProc(String software){
ProcessInformation processInformation = new ProcessInformation();
StartupInfoA startupInfo = new StartupInfoA();
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_SHOWNORMAL;
Kernel32.INSTANCE.CreateProcessA(software, null
, null
, null
, true
, 0
, null
, null
, startupInfo
, processInformation);
}
}
答案 0 :(得分:1)
getFieldOrder
方法的目的是提供字段在表示Structure
的类中出现的名称和顺序。您需要执行以下操作:
<强> PROCESSINFORMATION 强>
替换getFieldOrder
方法,如下所示:
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[] { "hProcess", "hThread", "dwProcessId", "dwThreadId" });
}
与StartupInfoA
类似,请按照StartupInfoA
中显示的顺序返回一个包含字段名称的列表。