Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

How to disable IPv6 support in a Java program

Answer:

To completely turn off IPv6 support in Java, you can set the system property "java.net.preferIPv4Stack", e.g.

# java -Djava.net.preferIPv4Stack=true ...

How to get the hostname of a local machine using Java

Answer:

The following codes illtsraeted how to get the hostname of the local machine using Java.

import java.net.InetAddress;

public class Test {

    public static void main(String[] args) {

        try {
            InetAddress addr = InetAddress.getLocalHost();

            // Get hostname
            String hostName = addr.getHostName();

            System.out.println(hostName);

        } catch (Exception e) {}
    }
}

Split on specific characters on Bash Script's for loop

Answer:

If you want to Bash script's for loop to split on non white-space characters, you can change the IFS, e.g.

#!/bin/bash
IFS='-'

list="a-b-c-d-e"

for c in $list; do
    echo $c
done

The above script will split on the character '-'

E.g.

# ./test.sh

a
b
c
d
e

How to uncompress a WAR file using command

Answer:

To un-compress a Java Web Archives file (*.war), you can use the command

# jar xvf my-app.war

Print to standard output in different scripting languages

Answer:

To print a string of "Hello World" to the standard output in different scripting languages

1. Perl

print "Hello, World!";

2. Python

print('Hello, World!')

3. PHP

echo "Hello, World!";

4. Ruby

puts "Hello, World!"