1

I want to execute the Java command: java -jar cli.jar <arg> <arg> from inside a PowerShell script. How do I pass the command line arguments passed to the PowerShell script to the Java command inside the script?

2
  • What is your question? How to write a powershell script that calls a java program? Or how that java program can react to the arguments provided to it? Commented Nov 5, 2016 at 9:38
  • 1
    I think the question is how to convey the arguments passed by the caller of the script to the java program to be executed. Commented Nov 5, 2016 at 12:43

3 Answers 3

6

If you want to pass commandline arguments to powershell script, you can use $args (this inbuilt variable will contain the arguments passed in the commandline)

foreach ($arg in $args) {
    "cli argument " + $arg
}
Sign up to request clarification or add additional context in comments.

Comments

1

Solution 1

Your script ps1

    param (
        [string[]]$ListParam
    )

    [string[]]$ListParamAll= "-jar", "cli.jar"
    $ListParamAll+=$ListParam

    start-process  "java"  -ArgumentList $ListParamAll

How to call this script:

    cli.ps1 -ListParam dir,-t,i:\app

Comments

1

When you call your program like you did your application would receive three parameters, namely:

  • dir
  • -t
  • i:\app

When you write your application you would normally write a main method that takes an arg array like this:

public static void main (String[] args)
{
   // Code
}

To answer your question, the args array would now contain your three parameters. So if you would change your main method to:

public static void main (String[] args)
{
   for (String arg : args) System.out.println(arg);
}

You would see your input parameters on the command line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.