用VBScript编译Java程序?(NetBeans)

用VBScript编译Java程序?(NetBeans),java,netbeans,vbscript,jar,Java,Netbeans,Vbscript,Jar,我创建了一个Java程序,在单击按钮后运行VBSscripts examplescript.vbs 如何编译这些vbs文件,然后调用它们在程序代码中运行?我已经排除故障好几天了,找不到答案。我再次需要能够运行这些脚本,有一次我创建了一个输入流,但无法将其作为vbs文件。希望我在这里忽略了一些东西 编辑: 这就是我现在拥有的。使用此代码,我收到错误“Windows脚本主机。没有用于文件扩展名的脚本扩展名”。BufferedInputStream@4e34904“” 您可以按如下方式运行VBScri

我创建了一个Java程序,在单击按钮后运行VBSscripts

examplescript.vbs

如何编译这些vbs文件,然后调用它们在程序代码中运行?我已经排除故障好几天了,找不到答案。我再次需要能够运行这些脚本,有一次我创建了一个输入流,但无法将其作为vbs文件。希望我在这里忽略了一些东西

编辑: 这就是我现在拥有的。使用此代码,我收到错误“Windows脚本主机。没有用于文件扩展名的脚本扩展名”。BufferedInputStream@4e34904“”


您可以按如下方式运行VBScript

  Runtime.getRuntime().exec( "wscript path/to/examplescript.vbs" );

如前所述,VBScript是一个脚本,不需要编译。如果要运行用VBScript编写的代码,可以通过以下方式执行:

此示例使用VBScript获取运行Microsoft Windows的计算机的主板序列号:

try {
    // Create a temporary script file named MBSerialxxxxxxxx.vbs
    File file = File.createTempFile("MBSerial",".vbs");
     // Delete the temporary file when virtual machines terminates
    file.deleteOnExit();  
    try (FileWriter fw = new java.io.FileWriter(file)) {
        String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                + "Set colItems = objWMIService.ExecQuery _ \n"
                + "   (\"Select * from Win32_BaseBoard\") \n"
                + "For Each objItem in colItems \n"
                + "    Wscript.Echo objItem.SerialNumber \n"
                + "    exit for  ' do the first cpu only! \n"
                + "Next \n";
        fw.write(vbs);
    }

    // Run the VBScript file....
    Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());

    // Read in any output to the command window.
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = input.readLine()) != null) {
        // display the output...
        System.out.println(line.trim());
    }
    input.close();
}
catch(IOException e){ 
    e.printStackTrace(); 
}

vbs是一个脚本,无法编译。您可以将您尝试过的最新代码传递到WScript.exe或CScript.exe吗?您的问题让我很难过,但在我们可能回答之前-为什么?这听起来像是,您需要在哪里运行VBScript?目前我已经尝试了getResourceAsStream的许多变体。但是,当传递到getRuntime().exec(wscript inputstreamvariable)时,我收到一个错误,它不是vbs文件。是的,直到我在构建后尝试引用它时才起作用。因此,无法获取现有的.vbs文件,将其包含在程序中,然后引用其路径?@Phasezaa,是否尝试将VBScript程序“导入”到java中?就像导入和使用其他JAVA类一样?是的,这就是我要做的。用getRuntime()在我的java程序中单击一个按钮就开始执行。exec()我的问题不同。绝对……你可以将这些VBSCript文件嵌入到你的项目中,或者从你决定与应用程序打包的任何文件夹中引用它们。您真正需要的上述代码的实际重要部分是:
processp=Runtime.getRuntime().exec(“cscript//NoLogo”+file.getPath())。这就是运行VBScript文件的原因,因为它运行一个名为cscript的实用程序,该实用程序最终运行VBScript文件。您很可能有
processp=Runtime.getRuntime().exec(“cscript//NoLogo”+“resources/myscript.vbs”)
try {
    // Create a temporary script file named MBSerialxxxxxxxx.vbs
    File file = File.createTempFile("MBSerial",".vbs");
     // Delete the temporary file when virtual machines terminates
    file.deleteOnExit();  
    try (FileWriter fw = new java.io.FileWriter(file)) {
        String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                + "Set colItems = objWMIService.ExecQuery _ \n"
                + "   (\"Select * from Win32_BaseBoard\") \n"
                + "For Each objItem in colItems \n"
                + "    Wscript.Echo objItem.SerialNumber \n"
                + "    exit for  ' do the first cpu only! \n"
                + "Next \n";
        fw.write(vbs);
    }

    // Run the VBScript file....
    Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());

    // Read in any output to the command window.
    BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = input.readLine()) != null) {
        // display the output...
        System.out.println(line.trim());
    }
    input.close();
}
catch(IOException e){ 
    e.printStackTrace(); 
}