Recent Notes
Displaying keyword search results 21 - 30
Created by alfa on June 01, 2011 13:08:36
Last update: June 01, 2011 13:08:36
Code:
public class SplitTest {
public static void...
Output:
Split 1:
'a'
' b '
' c '
'd '
S...
Created by freyo on May 23, 2011 14:30:18
Last update: May 23, 2011 14:31:08
There are two distinct ways to process XPath: with namespace and without namespace. The code is different depending on whether the parser is namespace aware.
Code without namespace:
import java.io.*;
import javax.xml.parsers.*;
...
code with namespace:
import java.io.*;
import java.util.Iterator;
...
XML without namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
XML with namespace:
<?xml version="1.0" encoding="UTF-8" standalone="n...
The same XPath expression works for both XML files when the parser is not namespace aware.
When the parser is namespace aware, you have to adjust the XPath accordingly depending on whether the XML has namespace declarations: " /test-license/licensee/name/text() " works for the XML file without namespace, while " /p:test-license/p:licensee/p:name/text() " works for the XML file with namespace.
Created by alfa on April 11, 2011 21:40:51
Last update: April 11, 2011 21:40:51
Integer.bitCount counts the number of one-bits in the two's complement binary representation of the specified int value. Example code:
public class CountOnes {
public static void...
For long , there's Long.bitCount()
Created by alfa on April 06, 2011 12:48:44
Last update: April 06, 2011 12:48:44
package com.demo.io;
import java.io.*;
...
Created by Dr. Xi on April 05, 2011 08:04:37
Last update: April 05, 2011 08:11:37
There's no difference between a Java HTTP client and a Java HTTPS client. Ignore JavaWorld Java Tip 96 , it's way too old. The following code gets an HTTP page as well as an HTTPS page.
import java.io.*;
import java.net.*;
pub...
There's one catch . If you are using the code on a test server with a self-signed certificate, it fails. In that case, I would suggest that you download the certificate from the server and import it to your keystore as a trusted key. You may also need to add a subject alternative name to the certificate if the host name does not match the certificate.
You may also choose to use a custom TrustManager and HostnameVerifier to ignore the certificate verification errors.
Created by Dr. Xi on March 29, 2011 16:06:57
Last update: April 01, 2011 12:33:52
This utility class retrieves SSL certificates from the server and print them out to the stdout. The output can be saved to a file and imported to a Java keystore. This is useful in your test environment where the SSL certificate is self-signed.
import java.io.InputStream;
import java.io.Outp...
Retrieve and import the a certificate:
E:\test>java RetrieveSSLCert 192.168.69.144 8081 >...
Created by meiu on March 31, 2011 20:05:53
Last update: March 31, 2011 20:05:53
The Java exclusive or operator is ^ . This example uses it to reverse an integer array:
public class ExclusiveOrExample {
public st...
Created by meiu on March 31, 2011 19:54:05
Last update: March 31, 2011 19:54:05
With StringBuffer/StringBuilder:
public class ReverseString {
private static...
Without StringBuffer/StringBuilder:
public class ReverseString {
private static...
Created by Dr. Xi on January 14, 2010 00:28:27
Last update: March 30, 2011 15:37:44
A task that a Java developer does so frequently is to find out where a certain class can be found - to resolve compilation errors, classpath issues, or version conflicts of the same class introduced by multiple class loaders. A long while back I wrote a simple Perl script to perform the task. Later I was informed that there are Swing based Jar Browser and Jars Browser . Then, there are a couple of shell one-liners:
# one liner 1 find -name "*.jar" -print0 | xarg... But all of them share the same problem: if a class is in a jar nested in another jar, it cannot be found. Such is the case for a class inside a jar under the WEB-INF/lib directory of a...
Created by Dr. Xi on March 30, 2011 13:43:05
Last update: March 30, 2011 13:45:27
Method 1 - use javap with verbose flag:
$ javap HelloWorld -verbose | head
Compiled fro...
Method 2 - use a utility class:
import java.io.*;
import java.nio.ByteBuffer;
...
According to the VM Spec , a Java class file has this structure:
ClassFile {
u4 magic;
...