Running file through a program with arguments - java

My issue here is that I can't seem to pass arguments through my program. I'm not sure how to explain it clearly but here it goes:
I want to be able to run a script file through a custom interpreter with arguments.
For example:
AtomScript.exe main.atom -> Program runs
When I want to run it through a batch file:
main.atom -> Program runs
Now when I want to pass arguments through the program using the AtomScript command in a batch file:
AtomScript.exe main.atom arg1 arg2 -> Program runs with arguments
The arguments are passed. But when I want to use the simple main.atom command to run it with arguments it doesn't work:
main.atom arg1 arg2 -> Program runs without arguments
I'm not sure what to do at this point. Any suggestions?

main.atom is not an executable, so it can't take any parameters
Windows knows, which program has to be started and does so (ignoring any parameters)
Only if you start the executable directly, you can define parameters (the first one being main.atom)
for example:
C:\>assoc .txt
.txt=txtfile
C:\>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
assoc defines, what filetype that extension is ("txtfile")
ftype defines, what has to be done for this filetype (start notepad)
You can see, there is just one parameter %1 (the filename (main.atom)).
Of course you could expand it with more parameters (needs to be run as administrator):
ftype txtfile=%SystemRoot%\system32\NOTEPAD.EXE %*
(which makes not much sense with this example), but I don't recommend it (it's just done on your computer). Better use the "official way": in the batchfile do exactly the same as on the commandline: start the executable with the desired parameters:
AtomScript.exe main.atom arg1 arg2

Related

Passing optional jvm arguments to java thru batch and shell scripts in command line

Got a java class with main method, say com.foo.Bar. But there are two optional vm(system) arguments for this class, say key1, key2.
Created a batch & shell shell scripts to call the above programs as below respectively.
Contents of Batch File(barRunner.cmd)
#echo off
set CLASSPATH=/bla/;/bla/
java -cp %CLASSPATH% com.foo.Bar %*
Contents of Shell script(barRunner.sh)
export CLASSPATH=/bla/:/bla/
java -cp $CLASSPATH com.foo.Bar $#
Now user calling in the below manner, but vm arguments cannot be read by Bar class
barRunner.cmd -Dkey1=value1
or
./barRunner.sh -Dkey2=value2 -Dkey1=value1
Suspect that the vm argument is passed after the class.
How to pass the vm arguments so that they are available before class name?
EDIT:
Have already tried changing script as below and it worked. But the issue if the class has program arguments.
java -cp %CLASSPATH% %* com.foo.Bar
Also aware of JAVA_OPTS, but it is a bit tedious for naive users; I mean, multiple commands to be run(set JAVA_OPTS in one command & and call the script in another line) and hesitant to use that way.
So thought of checking with forum if there is better way to achieve both vm & program arguments in single line and both args are optional.
VM arguments should be passed before the class name otherwise the arguments will become your program arguments so your command should be:
java -cp $CLASSPATH "$#" com.foo.Bar
With the above command, you could run with ./barRunner.sh -Dkey2=value2 -Dkey1=value1.
Now a slight issue is that all the arguments are passed as VM arguments. You may also want to pass some arguments to your program. To do that, you could do something like this:
java -cp $CLASSPATH $JVM_ARGS com.foo.Bar "$#"
With the above command, you could pass both JVM arguments and your program arguments like,
JVM_ARGS="-Dkey2=value2 -Dkey1=value1" ./barRunner.sh programArg1 programArg2

run existing java program with cmd

I have a running java program which converts a json file into another file format. Everything works great.
For the implementation I decided to use the MVC pattern.
Now I want to implement the whole conversion routine so that I can use a command prompt but I never worked with that and don't know how to achieve this at all.
My thoughts were:
Open cmd and navigate to the main.java-file.
Print out the whole possibilities (the user should be able to enter the dir of the source file and the target dir, the user should be able to choose the target format).
If everything has been entered by the user, the conversion routine should be started by pushing ENTER.
Help would be really nice. For the moment I just know how to compile (javac helloWorld.java) and print "Hello World!" by exeuting a program with java helloWorld...
The apache commons cli project provides utilities for parsing command line arguments and providing help menu. This makes it pretty simple to handle the args provided to your main method.
You will also need to provide scripts to assemble your class path. You can look at the maven app assembler plugin for ways of doing this.
The interaction between a shell/command prompt and the started Java program is very similar to the way it works in C programs*. The main() method receives arguments as strings from the command line (or from any other parent process which executes the java runtime).
In Java you get an array of strings. You need to decide yourself which string has what meaning.
public static void main(String[] arg) { // traditional or String ... args
System.out.println("You have " + arg.length + " arguments);
if (arg.length >= 1) System.out.println("First: " + arg[0]);
}
When starting a Java runtime with arguments, it is important to note, that arguments start after the class name (or the JAR name):
java -cp . package.Main arg0 arg1 ...
java -jar package.jar arg0 arg1 ...
The Java runtime also has an mechanism to specify system properties on the command line. This is done with the -D option.
java -Dverbose=yes -jar package.jar arg0 arg1 ...
java -jar package.jar -Dverbose=yes arg1 ... //not a system property but arg[0]
It is important, that this option is specified before the class/jar-name, otherwise it will not be processed by the runtime, but you will see another argument.
String verbose = System.getProperty("verbose", "false");
The reason why system properties are useful: you can use them for optional control, so you do not have to worry about recognizing arguments (there are a number of libraries out there which can do that but for small tools I think it is overkill).
BTW: there are some interactions between shells/prompts and started programs when using wildcards (* and ?) and whitespace/quoting - those are OS specific.
* in C the first argument args[0] is the program name, in java arg[0] is the first argument after the class name.

pass arguments from shell script to java code

I am trying to pass two arguments from shell script to java code.
java -classpath *various properties* -Xmx1025m MainClass "$Arg1" "$Arg2"
However, the application is exiting immediately from which I inferred that the arguments are not getting passed to java code.
The application is working fine when I test it in my eclipse using command line args, but when I invoke it from script, its failing. I even printed out the arg1, arg2 values before invoking the java command, they seem to be okay.
You're doing it right.
Get your Java program to start by looping through its arguments and printing them out, to confirm; but the command line is right. You could show the whole script if you'd like more detailed help.

passing parameter to a jar file which is called in a bat file?

I have created a jar which needs to be called in a bat file. I need to pass all the command line arguments recieved by bat file to the jar. Can anyone please help me out. I know this is a stupid question, but i dont have any idea about jar and bat. On net i am unable to find the combination of both. Also note that i dont know how to retieve the command line arguments in the bat file.
The parameters that you pass to your batch file can be accessed via
%1 %2 %3 ...
So if you call your batch like
C:>application.bat param1 param2 param3
then your java call inside the batch file should look like:
#echo off
java -cp app.jar com.example.Main %1 %2 %3
Inside you bat file you will have java command
just use java -jar helloworld.jar firstParam secondParam and I believe you can also use because that how we pass params to Maven and ANT etc
mybatchFile.bat -DfirstParam -DsecondParam
If you don't know how many parameters the users could pass to the batch file (if any) you need to get the no. of arguments whatever the user has passed, simply add the following snippet to your main method at the start
for (String s : args) {
// Iterate through String array in Java (args list)
System.out.println(s);
}
You can store all the arguments in an Arraylist to use it and iterate over it later

Passing command line arguments to javaws (Java WebStart) executable

Summary for those who might not want to read that much:
How do I do this: ? If we could pass ad-hoc command-line args to javaws, then javaws apps could be more like "1st class citizen" "ordinary application". E.g. we could pass filenames of files to be opened.
I would like to know if there is a way to pass "ad-hoc" command line arguments to the javaws executable. I already know how to specify them in JNLP file:
<application-desc main-class="org.example.ClassName">
<argument>...
While this can be used for what i want to accomplish, i treat this as a workaround.
I tried
javaws http://example.org/launch.jnlp <some CLI args here>
But "some CLI args here" were just ignored, i think.
If we could pass ad-hoc command-line args to javaws, then javaws apps could be more like "1st class citizen" "ordinary application". E.g. we could pass filenames of files to be opened.
Like e.g.
javaws [options] http://example.org/launch.jnlp my_file.jpg
Having arguments hardcoded in JNLP does not satisfy this use case.
There is nothing to confuse. The very straight answer which I tested and it is working perrfect.
To send the command line argument to any JNLP is very simple.
command prompt > javaws -open space arg1 space arg2 ... space arg n JNLP url
Ex:
c:\>javaws -open arg1 arg2 arg3 c:\myjnlp.jnlp
But remember one thing, We can get the -open also as args[0] in main method, so just avoid args[0]. Also it is not possible to avoid -open with direct argument prepixed with -.
the javaws executable accepts a run-option -arg <argument> which allows the called to send arguments to the application. I think these are appended to the arguments in the jnlp file.
So your call might look like this:
javaws [options] -arg my_file.jpg http://example.org/launch.jnlp
Edit: The above solution only works with OpenJDK's javaws which accepts the -arg parameter. Another option, according to this blog post, is that you can pass arguments to the JNLP file using URL query string parameters. This will obviously only work if you execute javaws with the full URL and won't work if you access it as a download link. I have not tested this so it may or may not work.
dynamically generate the webstart jnlp file with the parameters.
$ javaws [options] http://example.org/codewriter/write.jnlp?param1=my_file.jpg
the codewriter captures the query parameter and writes out the dynamic jnlp from the parameter

Categories

Resources