When Tomcat opens the VirtualBox API, the VBoxSVC process will start if it is not already running. If it starts from Java, it will bind to the ports that the Java process has bound, including the HTTPS port.
Since VBoxSVC will keep running until all processes that use it are shut down, this can prevent the Tomcat application from restarting again. When this occurs, we have to shut down all VMs, which is undesirable.
ENVIRONMENT INFORMATION:
OS: Solaris (5.11, oi_151a2)
VirtualBox: 4.1.12r77245
Java: 1.6.0_26
STEPS TO REPRODUCE:
Ensure that VBoxSVC is not running: ps -ef | grep VBox
Compile the test code (see below): pfexec javac -cp PATH/TO/vboxxpcom.jar VboxDemo.java
pfexec javac -cp /opt/VirtualBox/sdk/bindings/xpcom/java/vboxjxpcom.jar VboxDemo.java
Start the program with: pfexec java -cp .:PATH/TO/vboxjxpcom.jar -Dvbox.home==VIRTUALBOX_HOME_DIR -Djava.library.path=PATH_TO_VIRTUALBOX_LIBRARY VboxDemo
Example: pfexec java -cp .:/opt/VirtualBox/sdk/bindings/xpcom/java/vboxjxpcom.jar -Dvbox.home==/opt/VirtualBox -Djava.library.path=/opt/VirtualBox/i386 VboxDemo
This code will bind to port 4444, initialize the VirtualBox API, and then wait for input before terminating.
From a different terminal, confirm that VBoxSVC is running, and retrieve the process ID:
$ ps -ef | grep VBoxSVC
root 14965 1 0 16:14:32 ? 0:00 /opt/VirtualBox/amd64/VBoxSVC --auto-shutdown
Run this command, and note that binds to port 4444: pfexec pfiles <pid>
Code: Select all
import java.io.IOException;
import java.net.ServerSocket;
import org.virtualbox_4_1.IVirtualBox;
import org.virtualbox_4_1.VirtualBoxManager;
public class VboxDemo {
public static void main(String args[]) throws IOException {
System.out.println("Please stop VBoxSVC process before running the test,");
System.out.println("command to do test:");
System.out.println("java -cp DIROFCLASSFILE:PATHTO_vboxjxpcom.jar -Dvbox.home=ROOT_OF_VBOX -Djava.library.path=ROOT_OF_VBOX/i386 VboxDemo");
try {
// open port 4444
ServerSocket ss = new ServerSocket(4444);
// start one process from java (to compare to VBoxSVC)
Runtime.getRuntime().exec(
new String[] { "/usr/gnu/bin/sleep", "600" });
// ss.close();
} catch (IOException e) {
e.printStackTrace();
}
// use Vbox api, VBoxSVC process will be started by the api
VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
IVirtualBox vbox = mgr.getVBox();
vbox.getMachines();
// Now VBoxSVC should be started and binded to port 4444
// but the sleep process is not binded to port 4444
System.in.read();
}
}
Dinu Zabrauteanu