Thursday, November 22, 2012

Print a carriage return in java on windows




I need to print :q in a new line. I’m using a StringBuffer which prints a new line in this format

StringBuffer sBuffer = new StringBuffer();
sBuffer.append(String.format("Hello"));
sBuffer.append(String.format("\n"));
sBuffer.append(String.format(":q"));


The program works fine in Windows XP. :q will be printed in a new line when the program is executed.
Hello
:q


But in Windows 7, :q is printed in the same line.

Hello:q

The issue here is:
Different operating systems use different line separators.
In Java, \r is always carriage return, and \n is line feed. On Unix, just \n is enough for a newline, whereas many programs on Windows require \r\n. So you will have to modify the program like this to work on Windows 7.

StringBuffer sBuffer = new StringBuffer();
sBuffer.append(String.format("Hello"));
sBuffer.append(String.format("\r\n"));
sBuffer.append(String.format(":q"));

Wednesday, November 21, 2012

JNLP - Unable to launch application






Whenever I try to load my JNLP file, it is throwing the following exception.

com.sun.deploy.net.FailedDownloadException: Unable to load resource: http://a.b.c.d/MyProgram/MS.jnlp
            at com.sun.deploy.net.DownloadEngine.actionDownload(Unknown Source)
            at com.sun.deploy.net.DownloadEngine.getCacheEntry(Unknown Source)
            at com.sun.deploy.net.DownloadEngine.getResourceCacheEntry(Unknown Source)
            at com.sun.deploy.net.DownloadEngine.getResourceCacheEntry(Unknown Source)
            at com.sun.deploy.net.DownloadEngine.getResource(Unknown Source)
            at com.sun.deploy.net.DownloadEngine.getResource(Unknown Source)
            at com.sun.javaws.Launcher.updateFinalLaunchDesc(Unknown Source)
            at com.sun.javaws.Launcher.prepareToLaunch(Unknown Source)
            at com.sun.javaws.Launcher.launch(Unknown Source)
            at com.sun.javaws.Main.launchApp(Unknown Source)
            at com.sun.javaws.Main.continueInSecureThread(Unknown Source)
            at com.sun.javaws.Main$1.run(Unknown Source)
            at java.lang.Thread.run(Unknown Source)


Solution:

1. Start Java Control Panel (from windows control panel -> java, OR from Java folder)
2. On "General" tab click "Network settings..."
3. Choose "Browser Settings" instead of "Use Proxy server"
4. Click OK and you are done.

Thursday, November 1, 2012

Java Code to Replace character '+'


I need to replace the '+' sign in my string with some other string.

Code:

public class ReplaceString
{
            public static void main(String[] args)
            {
                        String st = "+++ My Data contains Plus sign";                       
                        String sData  = st.replaceAll("+", "Hello,");
                        System.out.println(sData);
            }          
}


But while executing it always throws error,

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^



Since '+' is a reserved character in regex, you need to escape it.The solution for this is to include the '+' character within square brackets or as \\+.

So the code looks like this:


public class ReplaceString
{
            public static void main(String[] args)
            {
                        String st = "+ My Data contains Plus sign";                       
                        String sData  = st.replaceAll("[+]", "Hello,");
                        System.out.println(sData);
            }          
}


or

public class ReplaceString
{
            public static void main(String[] args)
            {
                        String st = "+ My Data contains Plus sign";                       
                        String sData  = st.replaceAll("\\+", "Hello,");
                        System.out.println(sData);
            }          
}