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]);
.
.
.
}
Related
I made a Java program that you can run :
1 - Only with the GUI by launching the .jar .
2 - With the GUI and a console by launching a .bat .
java -jar "app.jar"
PAUSE
But now I would like to add the possibility to launch it ONLY with the console, that means without the GUI, I searched but didn't found how to do that.
I understand your question like: the application could work console-only, but it starts the ui always.
In that sense, look into you main method /class and simply check for the command line args. Maybe you simply add some -nogui parameter which you then use to put a condition around the launching of any gui components.
And given your comment: you have to compare your args like:
.... void main(String args[]) {
for (String arg : args) {
if (arg.equals(...
See here for more details regarding command line options.
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
So I'm building a Java CLI application that will have features similair to Windows's dir. I'm using the jopt-simple 4.9 library for my CLI parsing needs, and for acquiring options it seems pretty straightforward...
public static void main(String[] args) throws Exception {
OptionParser parser = new OptionParser("a::b?*");
OptionSet options = parser.parse(args);
parser.accepts("a", "Display all");
parser.accepts("b", "Bare output without metadata");
parser.accepts("?", "Displays this help prompt");
But what if I want to run my app without any args? Like I would run dir or ls to display local contents.
And what if I want to run my app without any options? Like if I'm just telling dir or ls which directory I want it to print out.
OK, so to achieve this is simply used the fact the JOpt also parses non-option arguments:
OptionParser parser = new OptionParser("a::b?*");
parser.allowsUnrecognizedOptions();
// In case the user specifies a path instead of just running the command
// locally, create an array out of the parsed directory strings.
String[] dirStringArray = options.nonOptionArguments().toArray(new String[options.nonOptionArguments().size()]);
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.
This is my first post on the forum, hope all of you guys are well.
I've got a issue using JiST/SWANS, the ad hoc simulator in java within eclipse.
I managed to load the API, (as an external JAR ofcourse) but Im basically having a problem integrating the runtime of JiST within eclipse.
After running the hello world im usually getting a stackoverflowerror exception, since it may need modifications within the runtime.
import jist.runtime.JistAPI;
public class hello implements JistAPI.Entity {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("simulation start");
hello t = new hello();
t.myEvent();
}
public void myEvent()
{
JistAPI.sleep(1);
myEvent();
System.out.println("hello world, t=" + JistAPI.getTime());
}
}
the website is: http://jist.ece.cornell.edu/index.html
Thank you in advance!
Actually you need to run Main.java within jist.runtime. But before rigth click Main.java, properties, Run/Debug settings, New,Arguments and type your class name (plain name no .java needed) in Progam arguments. This will tell the jist interface to translate your code using the rewriter and run it.
Examples:
To run hello.java type "hello"
To run aodvsim.java type: "jist.swans.Main driver.aodvsim"
If there are arguments needed type them after the clas name like: "jist.swans.Main driver.aodvsim -n 25 -f 2000x2000 -a grid:5x5 -t 10,600,60"
Wilmer Arellano
How well does SWANS work? Given that the documentation and code date back to 2005, I am not sure if this is the best platform to use.