How to pass parameters to a packaged java application (.exe) - java

I need to package a Java program so it's an exe that runs without depending on an installed Java. (from what I read Launch4J or jpackage can do the job)
And I need to pass parameters to this program program via the commandline.
Finally I need results generated by the Java program back in the calling application.
How do I do that?
Does a Java sitting in an exe have access to command line parameters?
I could do the data exchange via a file, eg sitting in the temp folder.
But I'd prefer not to use a fix-coded filename as it might happen that 2 threads call the Java at the same time...
Thanks for your thoughts!

An EXE generated by jpackage behaves in same manner as your original application except that all the Java / JVM path and options are unchangable, so the built in JVM is launching your class. All command line arguments are passed on.
It does not accept say new system property -Dprop=value but will let you pass all arguments you add to the command line to the main(String[]args) of your launch class such as:
yourapp.exe arg1 arg2
Don't use jpackage --arguments unless you want the arguments hardwired into the exe as well. See Packaging Tool User's Guide

Can only be obtained through the parameters declared by the main method
public static void main(String[] args) {
System.out.println("params1:" + args[0] + "params2:" + args[1]);
}

In command line:
java Arguments(name of the main class) arg0 arg1 arg2

Related

Does jar differentiate between program arguments and jvm arguments

I am trying to execute a jar file. It needs both program arguments and the jvm arguments. Do we need to do something different while passing the command line parameters in order for it to be able to differentiate them, or it will be handled automatically?
Currently I am using eclipse IDE, so I can configure it. However it will be finally run using command line only. Please let me know if something needs to be done differently.
Well, the JVM arguments are typed before using '-D', then after the jar file you'll type your program arguments:
java -Djvm_argument1=XXX -Djvm_argument2=YYY -jar myjar.jar my_argument1 my_argument2
I assume you're using java -jar file.jar? It's the same as if you used java MainClass. JVM arguments like -ea, -cp, -classpath, -X..., -D..., etc. are targeted to the VM while all other arguments are targeted to your app.

How to know the commands being passed when compiling with eclipse

I am new to Java programming, especially with eclipse. I would really like to know how the java programs are actually getting compiled, it would help me in knowing the Java command line interface better. So, I want to know if there is a way to know exactly which commands are being sent along with the switches to compile and run the java program?
I am sure that you are taking up a J2SE edition of Java programming which should be a fundamental to you. Let me start with giving you an glimpse of a Java Program:
class Greetings
{
public static void main(String args[])
{
System.out.println("The first character of the command line " + args[0]);
}}
So let's go thru what I meant by each line.
The class indicates beginning of the class which would contain all of
the methods to be into.
The "public static void main" indicates the main method which would contain the method to be executed.
And the characters found in the brackets of the only method is known as parameters.
Those parameters are passed from the command line to the program where they could be worked on depending on the program.
And with this, let me advise you to take up a read on this page for more on Java (J2SE).
http://www.w3resource.com/java-tutorial/java-program-structure.php
Happy Coding!
Unless there are some Eclipse logs that track that (check your installation folder), I doubt Eclipse shows it to you through the IDE. Compiling and running java mostly comes down to two commands:
javac
You compile by executing
javac -cp <your classpath/libs> <your source folder>
java
You run your program by executing
java -cp <classpath> <Class with main method> <main method arguments>

How to know java parameters from exe java apps

I have executable java apps (as "exe" file) that actually java wrapper executable.
is there a way to grep the java command / parameter from the exe java apps.
Thanks.
In case of a classical main-function you will find the commands in args parameter:
public static void main(String[] args) {
for (String s : args){
System.out.println(s);
}
}
Your question is not so clear, but a I try to answer.
If you are passing parameters from a wrapper exe to a java executable, you oblviosly know which parameters are passing in the exe.
If you need to know passed parameters in java application, instead, you probably have a main matod in this app. This method is declared as follow:
public static void main(String[] args){}
Where args array is exactly what you need: here you find the parameters passed to the java executable. All you have to do is to use this array.
You can connect to the java process using Java Visual VM and inspect some of the properties on the VM, like the system properties, memory settings and more. The tool is available in JDK bin/jvisualvm Visual VM page. You may have to try to connect to the available VMs from the list one by one to find yours. Eclipse appears as but I was able to see JVM args and system properties for it.

Java respawn process

I'm making an editor-like program. If the user chooses File->Open in the main window I want to start a new copy of the editor process with the chosen filename as an argument. However, for that I need to know what command was used to start the first process:
java -jar myapp.jar blabalsomearguments // --- need this information
> Open File (fileUrl)
> exec("java -jar myapp.jar blabalsomearguments fileUrl");
I'm not looking for an in-process solution, I've already implemented that. I'd like to have the benefits that seperate processes bring.
Since you are launching Java -> Java, you can use the existing classpath to set the classpath on the command line. This type of thing works really nice in the dev environment too.
ProcessBuilder selfLauncher = new ProcessBuilder(
"java", "-cp", System.getProperty("java.class.path"),
"com.my.mainClass" );
selfLauncher.start();
Update:
For executable jar files, you will have a classpath which is simply the relative path to the jar file itself. If you want the command line arguments, you will have to save them from main, and re-apply them when launching.
You can see this by packing the following program into a jar. I'm not actually sure what happens if you have jars inside the executable jar file. They probably show up in the classpath.
public class TestJarPath {
public static void main(String args[]) throws Exception {
for (String s : args)
System.out.print("[" + s + "] ");
System.out.println();
String cp = System.getProperty("java.class.path");
for (String s : cp.split(";"))
System.out.println(s);
}
}
For java -jar ..\tst.jar X, you get output like:
[X]
..\tst.jar
If all else fails, try writing a batch/shell script to launch your app. In windows you can pass %CmdCmdLine% to Java to get the entire command line.
See http://www.robvanderwoude.com/parameters.php
As far as I know is there no portable way to get this info. I found a property in the gcj runtime but I doubt this will cover a large percentage of the users.
I think the accepted practice is "Try and Pray" :
Hope it is on the path, (the path IS available, so that can be checked)
if not, check if JAVA_HOME is defined, and use that to find java.
if not check in the most likely places on all OS's you have received bug reports for.
Well, it is messy... porbably best to check for JAVA_HOME and the path and ask the user to configure a JVL explicitely if that fails.

Java Filepath Question

I am trying to finish a java program that uploads a file from a client machine to a webserver. The java program is executed with a bat script. I need to pass in a file name to the java program somehow since the filename is different every time. Or can i somehow use %1 instead of the filepath? I dont know.
Why not simply forward the parameters passed to the shell script to the Java application. I usually do something like this:
#!/bin/zsh
java -jar someapp.jar $#
This will pass all the arguments with which the script was executed to the java app and you can act upon them - as far as I understand you need only only - the file path. I'm not familiar with bat scripts, but I assume they have some similar way of passing args around.
What does the batch file look like that runs the Java program? You can indeed use parameters like this:
java -jar program.jar %1
If you put that line in a file runprogram.bat, then you could run it with:
runprogram somefilename.xyz
and somefilename.xyz will be passed to the Java program as a command line argument.
No they got it, if i could just pass the filepath as a parameter to the executed jar that would be awesome. Just need to figure out how to pass that parameter into a variable in the program....

Categories