Bind variables from Command line to test groovy Script - java

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.

Related

Making a jar that accepts names and values on command line

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

How do I pass arguments to a command inside a PowerShell script?

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.

Can command-line set Java system properties be distinguished from the defaults?

Is there a way to distinguish a Java system property which has been set from the command line using a -D option from a system property which got the same value by default?
E.g., this program
class Test {
public static void main(String[] args) {
System.out.println(System.getProperty("user.home"));
}
}
prints
/home/uckelman
for me regardless of whether I run it as
java Test
or
java -Duser.home=/home/uckelman Test
Is there anything which the JDK provides which I could test to distinguish these two situations?
One way is to get the command line arguments used to start the JVM and check the returned List if a given system property is set or not.
RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
System.out.println("COMMAND LINE ARGS:\n" + mx.getInputArguments());

Calling the main method of another class

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"};

How to replicate a SMTP incoming mail on JAVA args

I am trying to locally debug a remote issue where a java jar is processing an incoming mail:
java -jar /usr/local/bin/processmail.jar $*
I have the code within my eclipse and want to pass the email to it. Is there a way to capture the mail, such as in a logfile, or in a file and pass it to my Java project?
Cheers
Effectively there is the possibility, you need to have a main(String[] args) function into your main class in the jar, then the args that you pass in the command line after :
java -jar /usr/local/bin/processmail.jar oneemail#onedomain.com, otheremail#otherdomain.com
Here oneemail#onedomain.com and otheremail#otherdomain.com will be passed to the body of main method as part of args variable to be processed, of course you have to manage the implementation of the main method as you need for your intentions, something like:
... main(String[] args) {
doSomething(args[0]);
doSomething(args[1]);
.
.
.
}

Categories