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

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.

Related

difference between environment variables and System properties

iam using the below link to understand environment variables and system properties.
https://docs.oracle.com/javase/tutorial/essential/environment/env.html
The link says environment variables are set by OS and passed to applications.
When i fetch environment variables using System.getenv() it shows me lot of properties which i never set.
So it must be OS (im using macOS) which had set these properties.
Some of the properties in System.getenv() are MAVEN_CMD_LINE_ARGS, JAVA_MAIN_CLASS_1420, JAVA_MAIN_CLASS_1430.
My question is why would OS would like to set the java specific properties in environment variables? Ideally these should be set by JVM (in System.properties()).
P.S.: From whatever i have read on net i understand that environment variables are set by OS and System.properties() are set by JVM
Also if someone can point me to a good link on environment variable and System.properties it will be very helpful. Iam very confused between the two.
Environment variables is an OS concept, and are passed by the program that starts your Java program.
That is usually the OS, e.g. double-click in an explorer window or running command in a command prompt, so you get the OS-managed list of environment variables.
If another program starts your Java program1, e.g. an IDE (Eclipse, IntelliJ, NetBeans, ...) or a build tool (Maven, Groovy, ...), it can modify the list of environment variables, usually by adding more. E.g. the environment variable named MAVEN_CMD_LINE_ARGS would tend to indicate that you might be running your program with Maven.
In a running Java program, the list of environment variables cannot be modified.
System properties is a Java concept. The JVM will automatically assign a lot of
system properties on startup.
You can add/override the values on startup by using the -D command-line argument.
In a running Java program, the list of system properties can be modified by the program itself, though that is generally a bad idea.
1) For reference, if a Java program wants to start another Java program, it will generally use a ProcessBuilder to set that up. The environment variables of the new Java process will by default be the same as the current Java program, but can be modified for the new Java program by calling the environment() method of the builder.

Who resets JVM file.encoding back to original?

System.setProperty("file.encoding", "utf-8");
The comment below implies that file.encoding would be changed for all apps running on the same JVM, however, I don't observe this kind of behaviour.
Setting a system property programmatically will affect all code running within the same JVM, which is hazardous, especially when discussing such a low-level system property.
I have read this question and understand that there are many issues with caching and Java 1.5
Setting the default Java character encoding?
Please, now consider the following code:
public class FileEncodingTest {
public static void main (String[] args) {
System.out.println(System.getProperty("file.encoding"));
System.setProperty("file.encoding", "UTF-8");
System.out.println(System.getProperty("file.encoding"));
}
}
Then I create a jar-file using Eclipse and Java 1.6 set in project configuration.
Then I run jar-file with Java 1.7 and all this happens under Windows 7.
java -jar FileEncodingTest.jar
Cp1251
UTF-8
java -jar FileEncodingTest.jar
Cp1251
UTF-8
So who and why resets the value of file.encoding back to Cp1251?
UPD:
Anyone can explain or provide a link which explains step-by-step what happens in terms of JVM, processes when I type java -jar MyClass.jar?
you started 2 vm's. one with each "java -jar" command.
you can change the encoding your projects uses by editing the project properties in eclipse.
but note that when you hardcode stuff that relies on the fileformat and another project uses your implementation there will be problems. thats what the comment means.
Just like you open an IE browser, it goes to homepage at first. If you visit another website, then you open another IE, it will still be the homepage.
JVMs are quite similar. 2 different processes of java program use different JVMs. It means when the program ends, the file-encoding property will be default again.

Adding environment variables using java program and able to see when I open environment tab from My Computer

I want to write java program which adds environment variables and when I open environment tab from MyComputer,then I should be able to see that...
This is what I have tried,but it gives java.lang.unsupportedexception
Map env = System.getenv();
env.put("abc", "pqr");
And One more try is below,it is not giving any error...But I can not see any value added when I open environment variables tablenter code here from My Computer.But When i sysout "env" variable it gives me all paths including myone also...But I need same thing to be shwon in environment variables tab...
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe","/c","set");
Map env = processBuilder.environment();
env.put("abc", "pqr");
Please help me guys...Thanks in Advance...
There are two ways. The first one is to call the Windows API to change/set the environment variable. You must look for the right Windows API function and call it from Java. However calling any Windows API from Java is a topic on its own.
The second way is to call the setx.exe program with the correct arguments to set environment variables. Check the manual of the setx.exe program how to use it (and when it is installed) to set your environment variables.
In both ways you obviously restrict your Java program to run on Windows systems only.

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.

An easy bulletproof technique to check if the system has jre (windows)

Sorry my newbie question :P If I promp "java -version" in the cmd on a windows system, am I guaranteed that the system will be able to run .jar files if I don't get any error?
From the command line you should be able to invoke "java --version" which will return an error if java is not installed or the currently installed version information.
I guess the only guaranteed way to check for a JRE is to try to run a small Java program.
Or maybe not even that - I suppose conceivably a system could have only part of the Java standard library installed, in which case a small test JAR might work fine but a full program might not. Although I can't imagine why anyone would go to the trouble of setting a system up that way.
Why not run a small class file, which write a value to a file which you then check? If it fails, it doesn't work.
A good value might be the value of the java.version system property.
On Windows, you can check the registry at HKLM\SOFTWARE\JavaSoft\Java Plug-in. From there, each subkey is an installed JRE.
edit Here is C# code that will return an array of strings with the installed JRE's
public string[] GetInstalledJavas() {
// hold the registry subkeys that list the installed JRE's
string[] jres = null;
try {
RegistryKey myKey = Registry.LocalMachine;
myKey = myKey.OpenSubKey(#"SOFTWARE\JavaSoft\Java Plug-in"); // read-only
jres = myKey.GetSubKeyNames();
} catch (Exception myException) {
Console.Writeline(myException.ToString());
}
return jres;
}
Well, obviously not. You can put an empty file called java.bat anywhare in PATH, like C:\Windows\System32. Invoking "java" will not yield any errors but it doesn't mean there's a JRE installed.
I'd actually suggest, if you're only concerned about checking on windows machines, checking the registry for a handler for JNLP... that should guarantee the presence of a relatively recent JRE.

Categories