Passing command line arguments to javaws (Java WebStart) executable - java

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

Related

How can I execute .jar file from the terminal without specifying its name

How can I execute .jar file in folder from the Windows cmd without specifying its name.
I have tried below command (as there is only 1 jar in that folder I used *) but its not working(Error: Unable to access jarfile *.jar
).
java -jar *.jar
I am not sure it would be a good idea to just run everything in a directory, but you could:
FOR %A IN ("*.jar") DO (java -jar "%~A")
So what you appear to be asking is how to run the command
% java -jar somelongname.jar
as
% java -jar *.jar
to avoid some typing. That's not possible, because neither the Windows CMD shell or the java command is going to expand the *.jar wildcard pattern.
(On Linux / Unix / MacOS, the shell does wildcard expansion before passing arguments to a command. On Windows, it is the responsibility of the command to do this. In practice, it appears that the java command only expands wildcards in the arguments that are going to be passed to your application; see Stop expanding wildcard symbols in command line arguments to Java)
So if you want to avoid typing those pesky characters on Windows, you will need to do something like:
write a simple BAT file to run "java -jar somelongname.jar", or
write a clever BAT file to identify and run a JAR file that matches "*.jar", or
use Powershell.
For what it is worth, I think what you are trying to do is rather dangerous. This is a bit like typing "di*" to run the "dir". What if there is some other more dangerous command on the PATH that is going to match instead of "dir"?

Running file through a program with arguments

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

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.

How to run a exe file via unix by passing input parameter

I have created a exe file from jar via converter tools. Jar file was executing fine when I tried to run via unix by passing input parameters eg: java -jar SSS_Infinite.jar test.in 2
However after converting to exe I tried to run by passing input parameters via Unix but its not working and simply returns to the next line. I tried the below command in Unix cmd. Is there any other alternative to make it trigger ?
SSS_Infinite.exe test1.in 2
I assume you created executable for Windows platform, it will not work on *nix systems.
The simplest option will be to build little script that will accept parameters and pass them to java -jar, something like that:
#!/bin/bash
java -jar SSS_Infinite.jar $1 $2
where $1 and $2 are script arguments, see explanation here.
after you create that script and save it as say SSS_Infinite.sh, change its permissions:
chmod +x SSS_Infinite.sh
Then you'll be able to execute it like that:
./SSS_Infinite.sh test1.in 2

Expand wildcard in program's arguments

I have batch file which attempts to launch a java application:
java -jar myProgram*.jar
I would like the batch file to evaluate the wildcard * in order to find the program regardless of version numbers. So it should find myProgram1.jar, or myProgram438.jar and run it.
But this batch file yields:
Error: Unable to access jarfile myProgram*.jar
It looks like the arguments to java are not being processed by the shell. Is there a way to expand the wildcard in the arguments before passing them? I know that Bash has backtics which could do this. Is it possible to do in windows?
If there are more program*.jar in the folder you have to specify, which you want to start. The script starts the "last found":
#echo off&setlocal
for %%i in (myProgram*.jar) do set "jarProg=%%~i"
java -jar %jarProg%
You don't need "Cygwin" for this.

Categories

Resources