Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式传递windows凭据_Java_Windows_Credentials - Fatal编程技术网

Java 以编程方式传递windows凭据

Java 以编程方式传递windows凭据,java,windows,credentials,Java,Windows,Credentials,我有一个VB脚本,我需要传递用户名和密码 我想通过Java代码以编程方式运行这个VB脚本 是否有一种方法可以通过编程方式将Windows凭据传递到Java中的VB脚本?您可以在操作系统环境中获得凭据,然后从那里读取凭据: String credentials = System.getenv().get("encrypted_credentials_or_something"); 然后从Java运行您的命令。但是,Runtime.exec()在某些情况下不起作用: 当命令不在系统路径上时 当涉

我有一个VB脚本,我需要传递用户名和密码

我想通过Java代码以编程方式运行这个VB脚本


是否有一种方法可以通过编程方式将Windows凭据传递到Java中的VB脚本?

您可以在操作系统环境中获得凭据,然后从那里读取凭据:

String credentials = System.getenv().get("encrypted_credentials_or_something");
然后从Java运行您的命令。但是,Runtime.exec()在某些情况下不起作用:

  • 当命令不在系统路径上时
  • 当涉及争论时
  • 当您想要访问流程输出时
  • 当您需要能够终止进程时
  • 当您需要检查它是成功终止还是错误终止时(状态代码!=0-这就是您编写System.exit(int)来终止Java应用程序的原因。例如,System.exit(1)表示异常终止)
这就是为什么我创建了这个实用程序类来执行带有参数的外部进程和所有东西。这对我来说非常有效:

import java.io.*;
import java.util.*;

public class ExternalCommandHelper {

    public static final void executeProcess(File directory, String command) throws Exception {
        InputStreamReader in = null;
        try {
            //creates a ProcessBuilder with the command and its arguments
            ProcessBuilder builder = new ProcessBuilder(extractCommandWithArguments(command));
            //errors will be printed to the standard output 
            builder.redirectErrorStream(true);
            //directory from where the command will be executed
            builder.directory(directory);

            //starts the process
            Process pid = builder.start();

            //gets the process output so you can print it if you want
            in = new InputStreamReader(pid.getInputStream());

            //simply prints the output while the process is being executed
            BufferedReader reader = new BufferedReader(in);
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            int status = 0;
            //waits for the process to finish. Expects status 0 no error. Throws exception if the status code is anything but 0.
            if ((status = pid.waitFor()) != 0) {
                throw new IllegalStateException("Error executing " + command + " in " + directory.getAbsolutePath() + ". Error code: " + status);
            }

        } finally {
            if (in != null) {
                in.close();
            }
        }
    }

    //Splits the command and arguments. A bit more reliable than using String.split() 
    private static String[] extractCommandWithArguments(String command) {
        StringTokenizer st = new StringTokenizer(command);
        String[] cmdWithArgs = new String[st.countTokens()];

        for (int i = 0; st.hasMoreTokens(); i++) {
            cmdWithArgs[i] = st.nextToken();
        }
        return cmdWithArgs;
    }
}

VB脚本是否通过运行时.exec()执行?是的,我计划使用运行时.exec()运行它您不能在Runtime.exec调用期间将它们作为参数传递吗?这个问题将让您很好地了解Java中当前用户的哪些信息不幸的是,密码不是您可以从正在运行的系统中提取的信息。您需要提示itI不希望用户交互/用户输入。这段代码将在不同的用户机器上运行