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?
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
}
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
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.
Related
I have a java class, which has 3 methods and each method returns a String. I exported the .jar file out of that Java project. When I run the jar file from git bast using the command ./script1.sh, I get the output in the command window. My question is, how can I assign those values to the three variable that are in the script1.sh file. If I type echo ${"Source_branch"}, i should get output as "XXX_Source_Branch".
Here is my Java code :
package com.main;
public class Engine {
public static void main(String[] args) {
try {
getSourceBranch();
getDestinationBranch();
getDestEnv();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getSourceBranch() {
return "XXX_Source_Branch";
}
public static String getDestinationBranch() {
return "XXX_Dest_Branch";
}
public static String getDestEnv() {
return "XXX_Dest_Env";
}
}
The name of the jar file is Engine.jar.
And my script file is
runjar.sh
java -jar Engine.jar
Source_branch
Destination_brnach
Destination_env
pass data from java to bash
If you want to save data from java to the overlaying String, you will need to print that to System.out and read them from the shell.
Java code:
System.out.println("your source branch")
System.out.println("your destination branch")
Shell code
out="$(java -jar Engine.jar)"
sBranch="$(echo "$out"|sed -n '1p' /dev/stdin)"
destBranch="$(echo "$out"|sed -n '2p' /dev/stdin)"
pass data from bash to java
If you hard-code the Strings, java will just use the Strings. If you want to change that, you will need to modify your java code.
option 1 - arguments
If you want to pass the strings in the script using java -jar Engine.jar Source_branch Destination_brnach Destination_env, you can use them in your main method with the args parameter.
For example, you can print the first String in your main method using
System.out.println(args[0]);
If you do that, please test if args.length is greater than the highest argument you are accessing.
option 2 - System properties
If you want to get parameters without accessing args(independent of your main method), you will need to use System properties.
At first, change the script to be like that:
java -jar -DSource_branch=yourSourceBranch -DDestination_branch=yourDestinationBranch -DDestination_env=yourDestinationEnv
Engine.jar
Note that -D. That indicates that you are passing System properties. The syntax for this is -D<key>=<value> and it should be located between java and -jar.
Also, you will need to use System.getProperty() to access it from anywhere in your code.
For example:
public static String getSourceBranch() {
return System.getProperty("Source_Branch");
}
Create a .sh file with any file editor, like-
$> vi runJar.sh
Then, paste the below script ,
#!/bin/sh
java -jar Engine.jar
After saving the file, give execution permission -
$> chmod +x runJar.sh
Then run using
$> ./runJar.sh
Make sure the jar file and shell script file are in the same directory.
Relevant Links:
Java: Passing combination of named and unnamed parameters to executable Jar/Main Method
Passing arguments to JAR which is required by Java Interpreter
I understand how to pass strings from the command line to execute my main method:
java -jar myApp.jar "argument1"
My question is: is it possible to set up my main method in a way that would accept:
java -jar myApp.jar -parameter1 "argument1"
Here is my simple main method for context if you need it
public class myApp {
public static void main (String[] args){
System.out.println("Argument1: "+args[0]);
}
}
Thing is: whatever you pass on the command line goes into that args array. To be precise:
java xxx -jar JAR yyy
xxx: would be arguments to the JVM itself, like -Dprop:value for properties
yyy: are passed as arguments to your main method
So, when you pass "-parameter 'argument1'" then ... that is what you will see inside main!
In other words: the idea that some command line strings are "arguments"; and other are "-switches", or "--flags", or "-h" shortcuts ... you simply have to write the code to do all of that.
Luckily, there are plenty of libraries out there that help with that; see enter link description here
I am trying to call the main method of a function in another code.
The example from the command line I am trying to reproduce is:
java -cp stanford-ner.jar edu.stanford.nlp.ie.crf.CRFClassifier -loadClassifier ner-model.ser.gz -testFile jane-austen-emma-ch2.tsv
from here
In my code, I wrote:
String[] args = {"-loadClassifier ner-model.ser.gz",
"-testFile jane-austen-emma-ch2.tsv"};
CRFClassifier.main(args);
but when I try to execute this code, I get the following error:
Unknown property |loadClassifier ner-model.ser.gz|
Unknown property |testFile jane-austen-emma-ch2.tsv|
How can I call the main function from my code?
Every part of the command line, after the class name, is a separate argument. So the code should be
String[] args = {"-loadClassifier", "ner-model.ser.gz", "-testFile", "jane-austen-emma-ch2.tsv"};
I have a groovy script used in conjunction with GroovyScriptEngine:
public static void main(String[] args) {
GroovyScriptEngine gse = new GroovyScriptEngine(new String[] {"/home/user/tmp"});
Binding varSet = new Binding();
varSet.setVariable("testVar", "Hello World");
gse.run("printHello.groovy", varSet);
}
This is running just fine from java. The printHello.groovy starts keeping as already defined all the bound variables. The script "/home/user/tmp/printHello.groovy" is something like this:
println("${testVar} !!!")
What I want is to be able to test this script calling it from command line, but I haven't found a way to pass the binding variables to my script.
$ groovy printHello.groovy [???]
That could be very useful for testing.
You can just pass the arguments You need after the script invocation:
$ groovy groovyAuthDefault.groovy user pass
In the script all the parameters are accessible via args variable. More info.
Is that what You were looking for?
UPDATE
Found solution but it has some limitations, maybe it's possible to bypass them but don't know exactly how.
As I wrote above when You invoke script from command line You can pass arguments that are kept in args list. The problem lies in the fact that GroovyScriptEngine doesn't invoke the external script with it's main method - there's no args list so it fails with an MissingPropertyException. The idea is to set fake args.
java:
public static void main(String[] args) {
GroovyScriptEngine gse = new GroovyScriptEngine(new String[] {"/home/user/tmp"});
Binding varSet = new Binding();
varSet.setVariable("testVar", "Hello World");
varSet.setVariable("args", null); //null, empty string, whatever evaluates to false in groovy
gse.run("printHello.groovy", varSet);
}
printHello.groovy:
if(args) {
setBinding(new Binding(Eval.me(args[0])))
}
println("${testVar} !!!")
In printHello.groovy args is checked. If it evaluates to true it means that script was invoked from command line with arguments and a new Binding is set - evaluated from first element of arguments passed (plain groovy script extends groovy.lang.Script. If args evaluates to false it means that script was run with GroovyScriptEngine.
Command line invocation:
groovy printHello.groovy [testVar:\'hi\']
Exception handling might be added with other improvements as well. Hope that helps.
I have a java program can take a variable amount of parameters. Something like:
package other;
public class Main {
public static void main (String[] args) {
for (String arg: args) {/* do something */}
}
}
I want to run this java program from a .bat script.
"%JAVA_HOME%\bin\java" -cp "/some.jar;other.jar" other.Main %1 %2 %3
With this I can call my .bat script like
> myscript.bat arg1 arg2 arg3
This works if I have 3 arguments, but there can be a variable amount of arguments passed. How can I pass them all to the java program?
%* holds all arguments passed to a script.