Error: static class forcing UnsupportedOperationException

Error: static class forcing UnsupportedOperationException

我有一个 class,其中包含一个使用来自 JNA(Java Native Access)的 CreateProcess api 执行外部程序的静态方法。

PS: 我正在根据在 SO.here 中找到的这段代码进行制作。

我的麻烦是,当将 ProcessInformation class 和 StartupInfoA 作为静态 classes 用于在上述方法中使用时,这会强制 @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 的 class 中出现的名称和顺序。您需要执行以下操作:

进程信息

替换getFieldOrder方法如下:

  @Override
  protected List getFieldOrder() {
      return Arrays.asList(new String[] { "hProcess", "hThread", "dwProcessId", "dwThreadId" });
  }

StartupInfoA 类似,return 包含 StartupInfoA 中字段名称的列表,按它们出现的顺序排列。