我有一个包含静态方法的类,该类用于使用JNA(Java NAtive Access)中的CreateProcess
api执行外部程序。
PS:我是根据在SO中找到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);
}
}
最佳答案
getFieldOrder
方法的目的是提供名称和字段在代表Structure
的类中出现的顺序。您需要执行以下操作:
处理信息
替换getFieldOrder
方法,如下所示:
@Override
protected List getFieldOrder() {
return Arrays.asList(new String[] { "hProcess", "hThread", "dwProcessId", "dwThreadId" });
}
类似地,对于
StartupInfoA
,返回一个列表,该列表包含StartupInfoA
中字段的名称,并按它们出现的顺序排列。