You can obtain a Properties
instance of the JVM properties using System.getProperties()
; how would you go about using Java 8 code to print all properties to the console?
To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.
1. How to View Linux System Information. To know only the system name, you can use the uname command without any switch that will print system information or the uname -s command will print the kernel name of your system. To view your network hostname, use the '-n' switch with the uname command as shown.
Lambda Expressions were added in Java 8. A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
properties file located in users->appdata->locallow->sun->java>deployment and also directly putting key=value in runtime parameter in java control panel but not working. Edit: We use jeety server for deployment. And we have many properties file bundled with our souce code. What kind of application is it?
One solution:
public final class Foo
{
private static void printProperty(final Object key, final Object value)
{
System.out.println(key + ": " + value);
}
public static void main(final String... args)
{
System.getProperties().forEach(Foo::printProperty);
}
}
Rundown:
Properties
extends Hashtable<Object, Object>
which itself implements Map<Object, Object>
;Map
has a .forEach()
method whose argument is a BiConsumer
;BiConsumer
is a functional interface;printProperty()
of class Foo
happens to have the same signature as a BiConsumer<Object, Object>
: its "return value" is void
, its first argument is Object
, its second argument is Object
;Foo::printProperty
as a method reference.A shorter version would be:
public final class ShorterFoo
{
public static void main(final String... args)
{
System.getProperties()
.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
At runtime, this would not make a difference. Note the type inference in the second example: the compiler can infer that key
and value
are of type Object
. Another way to write this "anonymous lambda" would have been:
(Object key, Object value) -> System.out.println(key + ": " + value)
(not so) Side note: even though it is a little outdated, you really want to watch this video (yes, it's one hour long; yes, it is worth watching it all).
(not so) Side note 2: you may have noticed that Map
's .forEach()
mentions a default implementation; this means that your custom Map
implementations, or other implementations from external libraries, will be able to use .forEach()
(for instance, Guava's ImmutableMap
s). Many such methods on Java collections exist; do not hesitate to use these "new methods" on "old dogs".
@fge has missed one very short version that admittedly depends on the toString
implementation of Map.Entry
.
public class VeryShortFoo {
public static void main(String... args) {
System.getProperties().entrySet().forEach(System.out::println);
}
}
entrySet
is streamed and each Map.Entry
is printed with a reference to out.println
.Map.Entry
implementations of toString
generally return getKey() + "=" + getValue()
.Here's another one I quite like.
public class ElegantFoo {
public static void main(String... args) {
System.getProperties().entrySet().stream()
.map(e -> e.getKey() + ": " + e.getValue())
.forEach(System.out::println);
}
}
entrySet
is streamed again (this time explicitly with a call to stream
).Stream#map
performs a 1:1 conversion from elements of one type to elements of another. Here, it turns a Stream<Map.Entry>
in to a Stream<String>
.Stream<String>
is printed.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With