How to know java parameters from exe java apps - java

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.

Related

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

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

Open Jar without using java -jar with args

I am using a jar to run some code as a helper for a program (OSX). I want to open this jar programmatically, and have been using a ProcessBuilder to run it through the terminal.
However, I want to give the jar some arguments (specifically a file location, but that's irrelevant). I have using java -jar jarName arg, but this doesn't work with people who don't have Java tools installed.
I have tried to use open jarName --args arg, but the jar doesn't recognize the args.
As a test, I am just using the following code for now.
public static void main(String[] args) {
try {
// set a PrintStream to see the args presented
System.setOut(
new PrintStream(new FileOutputStream(new File(System.getProperty("user.home") + "/Desktop/argsTest.txt"))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(argsSize: "+args.length");
for (String s : args)
System.out.println(s);
}
I am fine with trying other methods of opening jars, so long as they are available on all up to date systems.
I have the JRE packaged in the application, is there a way to use that?
Bundle jre with with your program. Refer to Bundle JRE along with executable jar
or if you are using netbeans - it will allow you to test it first. https://netbeans.org/kb/docs/java/native_pkg.html
how to run - https://netbeans.org/kb/docs/java/native_pkg.html#check
Welcome on StackOverFlow!
Sadly, the answer is no, you can't do this. If you want to run some java code, from a jar or not, you will need to have java installed on the machine. Specifically, you need the JRE to have the JVM.
But you can provide java by your own when deploying your app. See this threads:
https://superuser.com/questions/745112/how-do-i-run-a-jar-file-without-installing-java
Running java without installing jre?
for more informations.

How can I use shell to transfer java method?

There is a Java demo :
package com.demo;
public class Demo {
public static void main(String[] args) {
System.out.println("hello world!");
}
}
How can I use shell to transfer it?
I am not very familiar with Linux. There is a demand:use shell to transfer java method.
Please tell me or give me a demo.
If by transfer you meant run (or execute), then simply do (you have to be in folder where Demo.class, compiled version, is)
java com.demo.Demo
but you have to have java on path, so test that first running
java -version
from console...
Or you can specify absolute path to java program....
You can write the same command into hello.sh if you want, there is not a big difference to have the command in script or execute it from command line.
If your script has to be more general, you should look for Linux scripting: Passing parameters and so on...

Is it possible to see System properties without writing a program?

Is it possible to display/print values of system properties without installing any program.
I am thinking perhaps one of the programs in jre/bin might do it.
Situation is that there is a locked-down system which is reporting strange values for "os.name" and I cannot install a tool to run.
You can print them all using the next command:
java -XshowSettings:all -version
JVisualVM is part of the jdk and can show the system properties of JVMs running on the same machine. If you want to see the values of no particular JVM you can just look at the system properties of JVisualVM’s own JVM.
This code snipped does print the system properties:
public static void main(String[] args) {
Properties properties = System.getProperties();
for (String name : properties.stringPropertyNames()) {
System.out.printf("%s=%s\n", name, properties.getProperty(name));
}
}
Check jconsole. to start jconsole , go inside your installation's bin directory and type ./jconsole on terminal.
I hope it may help you.

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>

Categories