VirtualBox Sdk Java - 从主机操作系统在客户操作系统内执行命令

时间:2015-02-02 15:12:17

标签: java virtualbox

我正在尝试从主机操作系统(Ubuntu 14.04)在客户操作系统(Ubuntu服务器)内创建用户。

这是我的代码

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);       

if(result == GuestSessionWaitResult.Start)
    process = guestSession.processCreate("adduser --disabled-password --gecos \"\" username", null, null, null, 0L);

ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
    System.out.println("started");

然而,这在ProcessWaitResult waitResult = process.waitFor(1L, time);失败了。我一直在

  
    

VBox错误:VirtualBox错误:rc = 0x80bb0005在guest虚拟机上找不到指定的文件(0x80bb0005)

  

我哪里错了?

2 个答案:

答案 0 :(得分:2)

我解决了。诀窍是发出命令的整个路径。我用which <command>得到了它。此外,我必须在单独的arraylist中传递命令的参数。

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);       

List<String> argumentsForProcess = Arrays.asList("--disabled-password", "--gecos", "'" + name + "'", username);

if(result == GuestSessionWaitResult.Start)
    createUser = guestSession.processCreate("/usr/sbin/adduser", argumentsForProcess, null, null, 0L);


ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
    System.out.println("started");
guestSession.close();
session.unlockMachine();

答案 1 :(得分:1)

我将可执行路径设置为空并将可执行路径放在参数[0]中

  List<String> argumentsForProcess = Arrays.asList( "/usr/bin/java", "-jar", "/home/webserver/vm60-docker/webserver/service-1.0.0.jar");

  process = guestSession.processCreate(null, argumentsForProcess, null, null, 0L);