Found 7442 Articles for Java

Get ClassPath from RuntimeMXBean in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

148 Views

RuntimeMXBean in the management interface for the runtime system of the Java virtual machine.RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();To get the class path, use the getClassPath() method −runtimeMX.getClassPath()The following is an example −Example Live Demoimport java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.util.Date; public class Demo { public static void main(String args[]) throws Exception { RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean(); System.out.println("Class path = "+runtimeMX.getClassPath()); } }OutputClass path = /home/cg/root/GNUstep/Library/Libraries/Java:/usr/GNUstep/Local/Library/Libraries/Java:/usr/GNUstep/System/ Library/Libraries/Java::/usr/share/java/mysql-connector-java.jar:.:/var/www/html/lib:/var/www/html/lib/dom4j- 1.6.jar:/var/www/html/lib/guava-18.0.jar:/var/www/html/lib/jackson-all.jar:/var/www/html/lib/jaxen- 1.1.4.jar:/var/www/html/lib/jcommon.jar:/var/www/html/lib/jdom2- 2.0.5.jar:/var/www/html/lib/jfreechart.jar:/var/www/html/lib/junit-4.12.jar:/var/www/html/lib/spymemcached- 2.10.3.jar:/var/www/html/lib/stax-1.2.0.jar:/var/www/html/lib/xstream-1.4.7.jar:/var/www/html/lib/gson- 2.3.1.jar:/var/www/html/lib/hamcrest-core-1.3.jarRead More

Get the JVM uptime from RuntimeMXBean in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

384 Views

RuntimeMXBean in the management interface for the runtime system of the Java virtual machine −RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();Let us get the JVM uptime using the getUptime() method −runtimeMX.getUptime()The following is an example −Example Live Demoimport java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.util.Date; public class Demo { public static void main(String args[]) throws Exception { RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean(); System.out.println("JVM Uptime = "+runtimeMX.getUptime() + " ms"); } }OutputJVM Uptime = 81 msLet us run the code again, to get the following output −JVM Uptime = 78 ms

Get the system properties from RuntimeMXBean in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

356 Views

RuntimeMXBean in the management interface for the runtime system of the Java virtual machine.RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean();To get the system properties, use the getSystemProperties() method −System.out.println("System properties from RuntimeMXBean: "+runtimeMX.getSystemProperties());The following is an example −Example Live Demoimport java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.util.Date; public class Demo { public static void main(String args[]) throws Exception { RuntimeMXBean runtimeMX = ManagementFactory.getRuntimeMXBean(); System.out.println("System properties from RuntimeMXBean: "+runtimeMX.getSystemProperties()); } }OutputSystem properties from RuntimeMXBean: {awt.toolkit=sun.awt.X11.XToolkit, file.encoding.pkg=sun.io, java.specification.version=1.8, sun.cpu.isalist=, sun.jnu.encoding=UTF-8, java.class.path=/home/cg/root/GNUstep/Library/Libraries/Java:/usr/GNUstep/Local/Library/Libraries/Java:/usr/G NUstep/System/Library/Libraries/Java::/usr/share/java/mysql-connector- java.jar:.:/var/www/html/lib:/var/www/html/lib/dom4j-1.6.jar:/var/www/html/lib/guava- 18.0.jar:/var/www/html/lib/jackson-all.jar:/var/www/html/lib/jaxen- 1.1.4.jar:/var/www/html/lib/jcommon.jar:/var/www/html/lib/jdom2- 2.0.5.jar:/var/www/html/lib/jfreechart.jar:/var/www/html/lib/junit-4.12.jar:/var/www/html/lib/spymemcached- 2.10.3.jar:/var/www/html/lib/stax-1.2.0.jar:/var/www/html/lib/xstream-1.4.7.jar:/var/www/html/lib/gson- 2.3.1.jar:/var/www/html/lib/hamcrest-core-1.3.jar, java.vm.vendor=Oracle Corporation, ... Read More

Displaying at most 10 characters in a string in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

497 Views

To display at most 10 characters in a string, work with the Formatter class.Import the following package for Formatter class in Java −import java.util.Formatter;Create a new Formatter object −Formatter f = new Formatter();Let us now display at most 10 characters in a string −f = new Formatter(); System.out.println("Displaying at most 10 characters: "+f.format("%.10s", "This is demo text!"));The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); String str = "This is demo ... Read More

Java Program to format to 2 decimal places in a 10-character field

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

177 Views

To format, use the Formatter class. Import the following package to work with the Formatter class in Java −import java.util.Formatter;Create a Formatter object and format to 2 decimal places in a 10-character field −Formatter f = new Formatter(); System.out.println(f.format("%10.2e", 3989.7886));The following is the complete example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%08d", 697)); f = new Formatter(); System.out.println(f.format("%03d", 9878)); f = new Formatter(); System.out.println(f.format("%10.2e", 3989.7886)); } }Output00000697 9878 3.99e+03

Java program to display table with columns in the output using Formatter

Samual Sam
Updated on 18-Oct-2024 11:57:24

1K+ Views

In this article, we will learn to display a table with columns in the output using Formatter in Java. The table will have headings and show different calculations of an array of floating-point numbers. The first column will display the original number, the second will display the number rounded up, and the third will display the number rounded down. Problem Statement Write a Java program to display a table with columns in the output using Formatter. Input double arr[] = { 1.7, 2.5, 3.1, 4.5, 5.7, 6.9, 7.7, 8.9, 9.1 }; Output The point list... Points1 Points2 Points3 1.70 2.00 1.00 2.50 ... Read More

Set a Minimum Field Width in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

2K+ Views

The minimum field width specifier is what you include between % and the format conversion code.To include a 0 before the field width specifier, pad with 0's. An integer between the % sign and the format conversion code acts as a minimum field width specifier.Let us see an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%08d", 697)); f = new Formatter(); System.out.println(f.format("%10d", 9878)); f = new Formatter(); System.out.println(f.format("%06d", 697)); } }Output00000697 9878 000697

The # format flag in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

350 Views

Apply the # flag to the %o, %x, %e, and %f format specifiers. If you want to display the hexadecimal number with a 0x prefix, then precede the %x specifier with #.Preceding the %x specifier with a #, the hexadecimal number will be printed with a 0x prefix.Let us see an example wherein $e is used. It includes a decimal point, even if the decimal digits are not present −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); System.out.println(f.format("%#e", 5F)); } }Output5.000000e+00

Add grouping specifiers for large numbers in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

239 Views

For using the Formatter class, import the following package −import java.util.Formatter;We can group specifiers as shown below −Formatter f = new Formatter(); f.format("%,.2f", 38178.9889);The above sets thousands separator and 2 decimal places.The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("%d", 50); System.out.println(f); f = new Formatter(); f.format("%,.2f", 38178.9889); System.out.println(f); } }Output50 38,178.99

Formatting a Negative Number Output with Parentheses in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

737 Views

A negative number output can be shown using the Formatter object −Formatter f = new Formatter(); f.format("%12.2f", -7.598); System.out.println(f);Try the below given code to format a Negative Number Output with Parentheses −Formatter f = new Formatter(); f.format("%(d", -50); System.out.println(f);The following is an example −Example Live Demoimport java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", 50); System.out.println(f); // negative number inside parentheses f = new Formatter(); f.format("%(d", -50); System.out.println(f); } }Output50 (50)

Advertisements