Traverse Process Tree of Process API in Java 9

raja
Updated on 08-Apr-2020 09:18:08

396 Views

Java 9 has improved Process API, and it helps to manage and control operating system processes. Before Java 9, it has been difficult to manage and control operating system processes using Java programs. Since Java 9, new classes and interfaces have added to control the operating system process through Java programs. New interfaces like ProcessHandle and ProcessHandle.Info have added, and also new methods have added to Process class.In the below example, we can traverse a process tree (children and descendant processes) of Process API.Exampleimport java.io.IOException; public class ProcessTreeTest {    public static void main(String args[]) throws IOException {       Runtime.getRuntime().exec("cmd");   ... Read More

Get ID of the Running Process in Java 9

raja
Updated on 07-Apr-2020 18:29:21

816 Views

Java 9 has added improvements to Process API for getting PID of running process, getting children and/or descendants of a process, and also added a new class that helps to list out all running processes, getting information about an arbitrary process, and traversing process tree. The information returned by these methods can be a snapshot of processes running on the OS.In the below example, we can get an ID of the running process by using the pid() method of ProcessHandle.Examplepublic class ProcessHandleTest {    public static void main(String args[]) {       ProcessHandle processHandle = ProcessHandle.current();       System.out.println("PID of running Process: " + ... Read More

Load Source Code into JShell in Java 9

raja
Updated on 07-Apr-2020 14:33:07

336 Views

JShell is an interactive tool for learning Java, and it is a REPL(Read-Evaluate-Print-Loop) that evaluates declarations, statements, and expressions.While leaving a JShell session, we want to reuse the code previously entered into a new session. This can be done by using the command: /open [File_Path]. This command will load all code and internal commands found in file[File_Path] supplied as an option.In the below code snippet, we can use the "/open [File_Path]" command to load source code from the directory with the ".jsh" extension.C:\Users\User>jshell | Welcome to JShell -- Version 9.0.4 | For an introduction type: /help intro jshell> ... Read More

Passing Static Methods as Arguments in PHP

AmitDiwan
Updated on 07-Apr-2020 14:20:40

802 Views

The same syntax used by is_callable and call_user_func can be used to pass static methods as arguments in PHP.To pass the static method, the below example can be used −Example Live Demo OutputThis will produce the following output −bool(true) my_func bool(true)

Pass Arguments from Array to Constructor in PHP

AmitDiwan
Updated on 07-Apr-2020 14:19:51

881 Views

The Reflection API can be used to pass arguments from array to constructor.ReflectionClass::newInstanceArgsThe above line creates a new class instance from given arguments −public ReflectionClass::newInstanceArgs ([ array $args ] ) : objectIt creates a new instance of the class when the arguments are passed to the constructor. Here, args refers to the arguments that need to be passed to the class constructor.Example Live DemoOutputThis will produce the following output −object(ReflectionFunction)#2 (1) { ["name"]=> string(6) "substr" }

Validate Domain Name in PHP

AmitDiwan
Updated on 07-Apr-2020 13:21:09

1K+ Views

The domain name can be validated using the below code in PHP −Example Live Demo $domain_name = 'https://tutorialspoint.com' is_valid_domain_name($domain_name)OutputThis will produce the following output −$domain_name = 'https://tutorialspoint.com' is_valid_domain_name($domain_name)In the above code, the ‘preg_match’ function is used to match the domain_name passed as an argument to the user-defined function ‘is_valid_domain_name’.

Install Imagick PHP Extension on Windows 10

AmitDiwan
Updated on 07-Apr-2020 13:18:50

309 Views

To install Imagick or Imagemagick on windows, follow the below mentioned procedure −Check the permissions on the .dll file. This will make sure that the Apache user has read access to the file.It is better to change the permission of the [PHP]/extension directory.In order to change the permission, follow the below steps −Right click on the file(s) or folder(s)Select "Properties"Select the "Security" tabClick on "Edit" button.Change the permission of user to Full Control.

Getting Size in Memory of an Object in PHP

AmitDiwan
Updated on 07-Apr-2020 13:17:10

2K+ Views

The memory_get_usage() function can be caked before and after allocating memory to the class created.class MyBigClass {    var $allocatedSize;    var $allMyOtherStuff; } function AllocateMyBigClass() {    $before = memory_get_usage();    $ret = new MyBigClass;    $after = memory_get_usage();    $ret->allocatedSize = ($after - $before);    return $ret; }Output will be the memory of object with respect to the environment setup.

Call Methods of Objects in Array Using array_map in PHP

AmitDiwan
Updated on 07-Apr-2020 13:15:01

1K+ Views

In PHP version 5.3, methods of objects in array can be called using the below code −$props = array_map(function($obj){ return $obj->getProp(); }, $objs);This will be slower than a ‘for’ loop since it invokes one function for every element −function map($obj) {    return $obj->getProperty(); } $props = array_map('map', $objs);Alternatively, for PHP versions before 5.3, the below code can be used −function map($obj) {    return $obj-> getProperty (); } $props = array_map('map', $objs); }The getProperty function will be called on all the objects and the specific property is displayed. Alternative −function encode_data($val){    if(is_array($val)){       return $val = ... Read More

HTML Select Option Value as Null Using PHP

AmitDiwan
Updated on 07-Apr-2020 13:12:47

1K+ Views

The short answer is no. POST/GET values are never null. The best they can be is an empty string, which can then be converted to null/'NULL' −Example Live Demoif ($_POST['value'] === '') {    $_POST['value'] = null; } echo'Null assigned';OutputThis will produce the following output −Null assigned

Advertisements