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"};
Related
I had batch script which executes TestRun class by taking jvm arguments as shown below
java -cp "./lib/*;./statoil.jar" -DURI=localhost:8080 -DOWUser=abc -DOWPassword=abc123 -DpipelineName=EDMStatOil -Ddatabase=edm -DproviderName=141Provider -DdestinationName=110EDM -DproviderWellName=Serno Grad com.statoil.rts.test.TestRun
But while running batch script getting error:
Error: Could not find or load main class Grad
I know it is treating Grad as class file. But how we can avoid this error while passing jvm argument with space?
Java doesn't care if there is a space in the JVM argument's value, but the terminal will split -DproviderWellName=Serno Grad into two command line arguments and pass those to the java executable.
You have to put quotes around the whole argument:
java "-DproviderWellName=Serno Grad"
In you batch file try setting the variable first and then pass that parameter to the actual command like these.
set WellName="Serno Grad"
java -cp "./lib/*;./statoil.jar" -DURI=localhost:8080 -DOWUser=abc -DOWPassword=abc123 -DpipelineName=EDMStatOil -Ddatabase=edm -DproviderName=141Provider -DdestinationName=110EDM -DproviderWellName=%WellName% com.statoil.rts.test.TestRun
OR
set WellName="Serno Grad"
java -cp "./lib/*;./statoil.jar" -DURI=localhost:8080 -DOWUser=abc -DOWPassword=abc123 -DpipelineName=EDMStatOil -Ddatabase=edm -DproviderName=141Provider -DdestinationName=110EDM -DproviderWellName="%WellName%" com.statoil.rts.test.TestRun
On my system either of them works fine.
try with escape characters -DproviderWellName="\"Serno Grad\""
java "$homeOption" -cp "$classPath" "com.civilizer.extra.tools.DataBroker" -import "$importPath"
If $homeOption is not empty, the command above works, but $homeOption is empty, it can't find the main class
Error: Could not find or load main class
Looks like Empty $homeOption parameter affects classpath string in a bad way; It's so strange behavior to me;
Anyone running into this issue and understanding why?
Edit:
In case that it works:
The actual command line is as follows;
com.civilizer.extra.tools.DataBroker is a Java class with main method, and it is included in that verbose classpath;
in this case, $homeOption is -Dcivilizer.private_home_path=/Users/bsw/.civilizer
java -Dcivilizer.private_home_path=/Users/bsw/.civilizer -cp /Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/classes:/Users/bsw/test/trysomething/civilizer/extra/lib/:/Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/lib/:/Users/bsw/test/trysomething/civilizer/target/extra com.civilizer.extra.tools.DataBroker -import
In case that it can't find the main class:
java -cp /Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/classes:/Users/bsw/test/trysomething/civilizer/extra/lib/:/Users/bsw/test/trysomething/civilizer/target/civilizer-1.0.0.CI-SNAPSHOT/WEB-INF/lib/:/Users/bsw/test/trysomething/civilizer/target/extra com.civilizer.extra.tools.DataBroker -import
As I mentioned, only $homeOption is empty; but it just makes the issue; BTW, even if $homeOption is empty, the class will run without a problem, but you know, the main method is missing in the first place in this case, it doesn't matter
You could resolve this by populating an array and passing that to the java command instead.
opts=( )
if [[ -n "$homeOption" ]]; then
opts+=( "$homeOption" )
fi
java "${opts[#]}" -cp "$classPath" "com.civilizer.extra.tools.DataBroker" -import "$importPath"
The issue you are seeing is because bash is passing a blank string to java as the first argument, and java is taking the blank string to be the class:
compare:
$ java foo
Error: Could not find or load main class foo
vs:
$ java ''
Error: Could not find or load main class
You can see that java prints the class name it can't find in the error, but your case, and my second case above, the class name is an empty string, so the class name is blank in the error message as well.
The reason my solution works is if the array is empty then bash won't pass in any empty arguments. And the array is created empty, and left empty unless $homeOption has a non-empty string.
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 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 am trying to call a java script function from java code.
Here is my Java code
public static void main(String[] args) throws FileNotFoundException {
try {
/**
* To call a anonymous function from java script file
*/
ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("javascript");
FileReader fr = new FileReader("src/js/MySpec.js");
engine.eval(fr);
} catch (ScriptException scrEx) {
scrEx.printStackTrace();
}
}
Here is my java script file:
(function() {
alert("Hello World !!!");
})();
But when I run main method of driver class it is giving me error as below:
Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "alert" is not defined. (<Unknown source>#2) in <Unknown source> at line number 2
at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:110)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at Java6RhinoRunner.load(Java6RhinoRunner.java:42)
at Java6RhinoRunner.main(Java6RhinoRunner.java:12)
What I know is that it need some script engine to execute it.
For that I added rhino.jar file in to my class path.But this is not working.
I an not getting how to solve this error.
Please help.Thanks in advance.
alert is not part of JavaScript, it's part of the window object provided by web browsers. So it doesn't exist in the context you're trying to use it in. (This is also true of setInterval, setTimeout, and other timer-related stuff, FYI.)
If you just want to do simple console output, Rhino provides a print function to your script, so you could replace alert with print. Your script also has access to all of the Java classes and such, so for instance java.lang.System.out.println('Hello'); would work from your JavaScript script (although it's a bit redundant with the provided print function). You can also make Java variables available to your script easily via ScriptEngine.put, e.g:
engine.put("out", System.out);
...and then in your script:
out.println('Hello from JavaScript');
...so that's a third way to do output from the script. :-)
See the discussion in the javax.script package documentation, in particular ScriptEngine#put, or for more complex cases, Bindings (and SimpleBindings) and ScriptContext.