#Experimenting with processbuilder, some help?

1 messages · Page 1 of 1 (latest)

iron spire
#

I'm running GUI that has a start and stop button.
The startbutton starts a process with a long function.
The stopbutton forecully stops this process.

I think the error message is telling me that when I start the process it says it's missing libraries?
While if I run it from my project itsself it runs just fine.

in GUI.java

    private void startButton(string PathToClass) {
        try {
            // Command to start the Side-process
            ProcessBuilder pb = new ProcessBuilder(JAVA_COMMAND, "-cp", PathToClass, "com.example.SideProcess");
            pb.inheritIO();

            SideProcess = pb.start();
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

in SideProcess.java

    public static void main(String[] args){
        System.out.println("is running...");
        SideProcess sidetask = new SideProcess ();
        sidetask.run();
    }

The error message:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/platform/win32/User32
        at com.example.WindowHandleUtils.findWindowContainingTitle(WindowHandleUtils.java:21)
        at com.example.WindowHandleUtils.<init>(WindowHandleUtils.java:15)  
        at com.example.Game.<init>(Game.java:17)
        at com.example.ColorDetectionUtils.<init>(ColorDetectionUtils.java:10)
        at com.example.SideProcess.<init>(SideProcess.java:10)
        at com.example.SideProcess.main(SideProcess.java:24)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.platform.win32.User32
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
        at java.base/java.lang.ClassLoader.loadClass(Unknown Source)        
        ... 6 more
narrow crowBOT
#

<@&987246487241105418> please have a look, thanks.

unkempt crane
iron spire
# unkempt crane before running it from processbuilder, run it from cmd

I get the same error when running it from CMD. As you can see it does excecute the println line "Is running". But in the run() function uses several libraries which are not included.

C:\Users\Falco\Desktop\Ard\OS\target\classes>java com.example.SideProcess
is running...
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/platform/win32/User32
        at com.example.WindowHandleUtils.findWindowContainingTitle(WindowHandleUtils.java:21)
        at com.example.WindowHandleUtils.<init>(WindowHandleUtils.java:15)
        at com.example.Game.<init>(Game.java:17)
        at com.example.ColorDetectionUtils.<init>(ColorDetectionUtils.java:10)
        at com.example.SideProcess.<init>(SideProcess.java:10)
        at com.example.SideProcess.main(SideProcess.java:24)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.platform.win32.User32
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
        ... 6 more
#

I'm working in Maven btw

iron spire
narrow crowBOT
iron spire
#

I thought maven already references the POM for the process but not sure 🙂

unkempt crane
# iron spire

in target, you should have a jar with dependencies, run it instead

iron spire
#

I created a jar with dependencies for the SideProcess class
Do you want me to call that JAR in the process I created here?
ProcessBuilder pb = new ProcessBuilder(JAVA_COMMAND, "-cp", PathToClass, "com.example.SideProcess");

#

The Jar runs fine so I guess it should work 🙂

iron spire
#

It appeared in my root folder instead of target folder somehow...

#

I'll try to start a process with the jar see what it does

iron spire
#

I thought I was using Maven?
How do I use Maven

#

What are the commands

#

It does work by the way

#

    private void Startbutton() {
        try {
            // Command to start the ArdyCakeBot process from the JAR file
            ProcessBuilder pb = new ProcessBuilder(JAVA_COMMAND, "-jar", PATH + \\Ard\\SideProcess.jar");
            pb.inheritIO();
    
            SideProcess = pb.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
#

I just call that jar from the start button

#

If it works it works amirite?

unkempt crane
iron spire
#

Im using visual studio code, the export jar button is from a plug-in for maven in Visual studio code, it let me select all the dependencies I include in the jar and then just generates it

#

Everybody saying I should use intelli J lol

#

Guess I'll have to try that

unkempt crane
iron spire
#

ok I'll try it that way thakns

unkempt crane
iron spire
#

it worked. However it doesnt stop when I terminate it with my following code:

    private void stopProcess() {
        if (SideProcess != null) {
            // Terminate the Sideprocess
            SideProcess.destroyForcibly();
        }
    }   
}
#

Should I instead just look in my processes and destroy the process that way?

That doesn't seem like the right way to do it

iron spire
#

I used destroy() instead and it worked