Search code examples
javaexceptionnetbeansjna

Error: static class forcing UnsupportedOperationException


I have a class that contains a static method for execute a external program using CreateProcess api from JNA (Java NAtive Access).

PS: I'm making based in this code found here in SO.

My trouble is that when put ProcessInformation class and StartupInfoA as static classes for use this in method described above, this forces a @Override of type:

        @Override
        protected List getFieldOrder() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

And I never have success for execute my project :-(. See:

enter image description here

So, i ask to you: - Exist some solution for it?

Here is how i'm making all process for obtain my goal:

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

}

Solution

  • The purpose of the getFieldOrder method is to provide the names and the order in which fields appear in a class that represents a Structure. You need to do the following :

    ProcessInformation

    Replace the getFieldOrder method as follows :

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

    Similarly for StartupInfoA, return a list containing the names of the fields in StartupInfoA in the order in which they appear.