Page 1 of 2

Please help me understand with simple examples of using API in Java.

Posted: 14. Sep 2022, 17:25
by KrekerMeister
Wrote a trial "code" and it doesn't work out right every time. What am I doing wrong? Libraries are included. I can't find a solution on the internet.

Re: Please help me understand with simple examples of using API in Java.

Posted: 14. Sep 2022, 18:44
by noteirak
Examples are provided here.

Re: Please help me understand with simple examples of using API in Java.

Posted: 15. Sep 2022, 21:45
by KrekerMeister
noteirak wrote:Examples are provided here.
Using a simple example, there is also an error here. Can you please tell me where the problem might be?

Re: Please help me understand with simple examples of using API in Java.

Posted: 15. Sep 2022, 21:50
by noteirak
Please follow the posting guidelines, especially the following bit:
Make sure you Include any code using the [ code ] tag if not too long, else attach the source file as a zip.
Make sure your code is readable/documented, can run without dependencies and is self-sufficient.
In case of a specific issue, you would provide a single class with the main method and the code to run to reproduce your issue.
Make sure you provide all output for any issues, including full stacktrace.
Screenshots make it impossible to help you effectively and only break the forum pages because of their sizes. I have removed them.

Re: Please help me understand with simple examples of using API in Java.

Posted: 15. Sep 2022, 22:06
by KrekerMeister
noteirak wrote:Please follow the posting guidelines, especially the following bit:
Make sure you Include any code using the [ code ] tag if not too long, else attach the source file as a zip.
Make sure your code is readable/documented, can run without dependencies and is self-sufficient.
In case of a specific issue, you would provide a single class with the main method and the code to run to reproduce your issue.
Make sure you provide all output for any issues, including full stacktrace.
Screenshots make it impossible to help you effectively and only break the forum pages because of their sizes. I have removed them.
Okay, here is the code:

Code: Select all

import org.virtualbox_5_2.*;

import java.util.List;

public class Main {

    public static void main(String[] args) {

        VirtualBoxManager vboxManager = VirtualBoxManager.createInstance("");
        try {
            vboxManager.connect("http://localhost:18083", "user", ""); // only if you are using WebServices Bindings
            IVirtualBox vbox = vboxManager.getVBox();
            for (IMachine vm : vbox.getMachines()) {
                if (MachineState.Running.equals(vm.getState())) {
                    System.out.println(vm.getName()+ " "+vm.getId());
                }
            }
        } finally {
            vboxManager.disconnect(); // only if you are using WebServices Bindings
            vboxManager.cleanup();
        }
    }

}
I get an error

Code: Select all

C:\Users\Yasral\.jdks\openjdk-18.0.2.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2.1\lib\idea_rt.jar=57296:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath "C:\Users\Yasral\IdeaProjects\untitled\out\production\untitled;C:\Program Files\Oracle\VirtualBox\sdk\bindings\xpcom\java\vboxjxpcom.jar" Main
Exception in thread "main" org.virtualbox_5_2.VBoxException: vbox.home Java property must be defined to use XPCOM bridge
	at org.virtualbox_5_2.VirtualBoxManager.createInstance(VirtualBoxManager.java:108)
	at Main.main(Main.java:9)

Process finished with exit code 1

Re: Please help me understand with simple examples of using API in Java.

Posted: 15. Sep 2022, 22:09
by noteirak
The code sample you are trying to run is for the WebService connection, but you are including the xpcom JAR into the classpath. Include the correct JAR and it should take you further.

Re: Please help me understand with simple examples of using API in Java.

Posted: 1. Oct 2022, 21:46
by KrekerMeister
noteirak wrote:The code sample you are trying to run is for the WebService connection, but you are including the xpcom JAR into the classpath. Include the correct JAR and it should take you further.
I use this code:

Code: Select all

import org.virtualbox_5_2.*;
import org.virtualbox_5_2.IMachine;
import org.virtualbox_5_2.IVirtualBox;
import org.virtualbox_5_2.MachineState;
import org.virtualbox_5_2.VirtualBoxManager;

public class Main {
    public static void main(String[] args) {
        VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
        mgr.connect("http://localhost:18083",null,null);
        IVirtualBox vbox = mgr.getVBox();
        System.out.println("VirtualBox Version: " + vbox.getVersion() + "\n");
        String m = vbox.getMachines().get(0).getName();
        System.out.println(m);
        mgr.disconnect();
        }
    }
I am getting this error

Code: Select all

Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/ws/WebServiceException
	at org.virtualbox_5_2.VirtualBoxManager.<clinit>(VirtualBoxManager.java:527)
	at Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: javax.xml.ws.WebServiceException
	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:521)
	... 2 more
Exception in thread "Thread-0" java.lang.NoClassDefFoundError: javax/xml/ws/Service
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1013)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	at org.virtualbox_5_2.PortPool.getPort(VirtualBoxManager.java:128)
	at org.virtualbox_5_2.PortPool.preinit(VirtualBoxManager.java:101)
	at org.virtualbox_5_2.PortPool.access$200(VirtualBoxManager.java:54)
	at org.virtualbox_5_2.PortPool$1.run(VirtualBoxManager.java:78)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.ClassNotFoundException: javax.xml.ws.Service
	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:521)
	... 14 more
WebService connected and VboxWebSrv launched. On ubuntu, the same code runs for me without problems.
And I have another question, so on Windows it will not be possible to use the COM interface and is it only for Linux?

Re: Please help me understand with simple examples of using API in Java.

Posted: 3. Oct 2022, 08:21
by noteirak
There are several JARs given by thevVirtualBox SDK ZIP. You want the big one (several MBs) with "ws" in it. Are you sure you are using the right one?
on Windows it will not be possible to use the COM interface and is it only for Linux?
COM is available on Windows, but it's MSCOM, instead of XPCOM. I don't have experience running the MSCOM code, so I can't help you personally.

Re: Please help me understand with simple examples of using API in Java.

Posted: 4. Oct 2022, 17:50
by KrekerMeister
Connected everything in the folder sdk\bindings\webservice\java\jax-ws

Re: Please help me understand with simple examples of using API in Java.

Posted: 20. Oct 2022, 21:30
by KrekerMeister
I managed to understand what the problem is, it turns out that the java version did not fit and did not run vboxwebsrv through administrator rights. Now the problem is when working with sessions. Here I run the following code, on a new machine that has not been used. I follow all the rules. I block, I make changes, I save, I unblock. But I don't understand what he wants. I am getting this error
Tried to block and then get a second session, same error.

Code:

Code: Select all

import org.virtualbox_7_0.*;

public class Main {
    public static void main(String[] args)  {
        VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
        mgr.connect("http://localhost:18083", "user", "password");

        IVirtualBox vbox = mgr.getVBox();
        IMachine mach = vbox.findMachine("Test #2");
        ISession sessi = mgr.getSessionObject();
        mach.lockMachine(sessi, LockType.Write);
        IMachine machine = vbox.findMachine("Test #2");
        ISession session = mgr.getSessionObject();
        mach.setCPUCount(2l);
        mach.saveSettings();
        sessi.unlockMachine();

        System.out.println("Done");
        mgr.disconnect();
    }
}
Error:

Code: Select all

Exception in thread "main" org.virtualbox_7_0.VBoxException: VirtualBox error: rc=0x80bb0002 The machine is not mutable (state is PoweredOff) (0x80bb0002)
	at org.virtualbox_7_0.IMachine.setCPUCount(IMachine.java:633)
	at Main.main(Main.java:15)
Caused by: org.virtualbox_7_0.jaxws.RuntimeFaultMsg: VirtualBox error: rc=0x80bb0002 The machine is not mutable (state is PoweredOff) (0x80bb0002)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:135)
	at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:189)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:276)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:104)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
	at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
	at com.sun.proxy.$Proxy35.iMachineSetCPUCount(Unknown Source)
	at org.virtualbox_7_0.IMachine.setCPUCount(IMachine.java:625)
	... 1 more

Re: Please help me understand with simple examples of using API in Java.

Posted: 20. Oct 2022, 23:19
by noteirak
You use the IMachine object you got from findMachine() which is a read-only object.
You need to use the IMachine from your session object (in the sessi var) that you get from getMachine(). That one is the mutable one.

Re: Please help me understand with simple examples of using API in Java.

Posted: 23. Oct 2022, 12:01
by KrekerMeister
Thanks a lot. Now faced with another problem - creating and connecting a dynamic hard disk to a new machine. Can you please suggest what I'm doing wrong?

Code: Select all

static void createmachine(VirtualBoxManager mgr, IVirtualBox vbox, String machinename) {
        IMachine machine = vbox.createMachine(null, machinename, null, "Windows81_64", null,null,null, "machine");
        ISession session = mgr.getSessionObject();
        vbox.registerMachine(machine);
        machine.lockMachine(session, LockType.Write);
        IMachine mach = session.getMachine();
        IMedium medium = vbox.createMedium("VDI","C:\\Users\\Yasral Jr\\VirtualBox VMs",AccessMode.ReadWrite,DeviceType.HardDisk);
        IProgress progress = medium.createBaseStorage(42949672960l, Collections.singletonList(MediumVariant.Standard));
        progress.waitForCompletion(-1);
        IStorageController storage = mach.addStorageController("SATA",StorageBus.SATA);
        storage.setPortCount(2l);
        mach.attachDevice("SATA",0,0,DeviceType.HardDisk,medium);

        mach.setCPUCount(2l);
        mach.setMemorySize(768l);
        IGraphicsAdapter grapchics = mach.getGraphicsAdapter();
        grapchics.setVRAMSize(48l);
        grapchics.setGraphicsControllerType(GraphicsControllerType.VBoxSVGA);
        IAudioAdapter audio = mach.getAudioSettings().getAdapter();
        audio.setEnabled(false);
        mach.saveSettings();
        //IMediumAttachment mediumAttachment = mach.getMediumAttachment("VDI",1,1);

        session.unlockMachine();
        vbox.registerMachine(machine);
    }
I get an error

Code: Select all

Exception in thread "main" org.virtualbox_7_0.VBoxException: VirtualBox error: rc=0x80bb0007 Storage for the medium 'C:\Users\Yasral Jr\VirtualBox VMs' is not created (0x80bb0007)
	at org.virtualbox_7_0.IMachine.attachDevice(IMachine.java:3451)
	at VirtBox.createmachine(VirtBox.java:29)
	at Main.main(Main.java:14)
Caused by: org.virtualbox_7_0.jaxws.RuntimeFaultMsg: VirtualBox error: rc=0x80bb0007 Storage for the medium 'C:\Users\Yasral Jr\VirtualBox VMs' is not created (0x80bb0007)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:135)
	at com.sun.xml.internal.ws.client.sei.StubHandler.readResponse(StubHandler.java:238)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:189)
	at com.sun.xml.internal.ws.db.DatabindingImpl.deserializeResponse(DatabindingImpl.java:276)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:104)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:77)
	at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:147)
	at com.sun.proxy.$Proxy35.iMachineAttachDevice(Unknown Source)
	at org.virtualbox_7_0.IMachine.attachDevice(IMachine.java:3443)
	... 2 more

Re: Please help me understand with simple examples of using API in Java.

Posted: 4. Nov 2022, 21:11
by KrekerMeister
It turned out, it turned out it was necessary to create a list. Question: how can I stop the program, before the end of the installation through unnatended?

Re: Please help me understand with simple examples of using API in Java.

Posted: 14. Nov 2022, 10:20
by noteirak
What do you mean by "the program" in this case? The VM process? The guest OS/system? The install process inside the VM? Something else?

Re: Please help me understand with simple examples of using API in Java.

Posted: 16. Nov 2022, 18:04
by KrekerMeister
During the installation of the OS, it is necessary to stop the execution of the Java program until the installation is completed and the guest OS can be used