JDK-4947206 : New line character (escape sequence) is not recognised in the command line args.
  • Type: Bug
  • Component: tools
  • Sub-Component: launcher
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-10-31
  • Updated: 2003-11-06
  • Resolved: 2003-11-06
Description

Name: gm110360			Date: 10/31/2003


FULL PRODUCT VERSION :
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)

FULL OS VERSION :
Microsoft Windows 2000 [Version 5.00.2195]

A DESCRIPTION OF THE PROBLEM :
If the command line argument (i.e, string value) includes new-line character('\n') then it is not being recognised as a single new-line ascii character. Rather, it is treated as two distinct ascii characters ('\' and 'n'). But if the same string is used in the java source code, instead of passing as command line argument, the new-line character is recognised as a single new-line ascii character.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the sample source code...
C:\> java Test "This\ncontains\nline"


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected the new-line character to be recognised in both command-line argument and java source hard-code string.

This is much like C/C++!!!!!
This is much like C/C++!!!!!
ACTUAL -
First time, the command line argument is not recognised. But the second if condition succeeds.

This ain't like C/C++!!!!!
This is much like C/C++!!!!!

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No errors or exceptions occurred.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
/* Source code STARTS */
class Test
{
	public static void main(String [] arrStrArgs)
	{
		String strTemp = arrStrArgs[0];

		if( "This\ncontains\nline".equalsIgnoreCase( strTemp ) )
		{
			System.out.println( "This is much like C/C++!!!!!" );
		}
		else
		{
			System.out.println( "This ain't like C/C++!!!!!" );
		}

		strTemp = "This\ncontains\nline";
		if( "This\ncontains\nline".equalsIgnoreCase( strTemp ) )
		{
			System.out.println( "This is much like C/C++!!!!!" );
		}
		else
		{
			System.out.println( "This ain't like C/C++!!!!!" );
		}

		System.exit( 0 );
	}
}


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I had to write a method to parse all the command-line arguments and replace the string "\\" + "n" with ascii character '\n'.

Following is the method:

/* Call this method like this:
strText = replace( strText, "\\" + "n", '\n' );
strText = replace( strText, "\\" + "t", '\t' );
*/

public static String replace( String strSource, String strFind, char chrReplace )
{
	// buffer to hold the target string after replacement is done.
	StringBuffer sbfTemp = new StringBuffer();

	try
	{
		// for each occurrence of strFind in strSource, replace it with chrReplace.
		int intIndex = strSource.indexOf( strFind, 0 );

		// check if there is any instace of strFind in strSource
		if( intIndex >= 0 )
		{
			// holds the index from where the search is supposed to happen.
			int intStart = 0;

			// size of the source string
			int intTotalSize = strSource.length();

			while( intStart < intTotalSize &&
			( ( intIndex = strSource.indexOf( strFind, intStart ) ) >= 0 ) )
			{
				// check if strFind is at the beginning... i.e., at index intStart
				if( intIndex == intStart )
				{
					/*
					 * starts with strFind...just append chrReplace
					 * to the target
					 */
					sbfTemp.append( chrReplace );
				}
				else
				{
					// append the sub-string...plus chrReplace
					sbfTemp.append( strSource.substring( intStart,  intIndex ) );
					sbfTemp.append( chrReplace );
				}

				// advance string index
				intStart = intIndex + strFind.length();
			}

			// append the last portion of the source string.
			sbfTemp.append( strSource.substring( intStart ) );
		}
		else
		{
			// strFind not found... just copy the text as it is.
			sbfTemp.append( strSource );
		}
	}
	catch( Exception expGeneral )
	{
		// in case of any exception, return the source string as it is.
		sbfTemp = new StringBuffer( strSource );
	}

	return sbfTemp.toString();
}
(Incident Review ID: 208529) 
======================================================================

Comments
EVALUATION Contrary to the bug filer's program, the behavior of Java is consistent with C/C++ because the effect being seen comes from the shell. I wrote a C program that echoed its first argument. ./a.out "This\ncontains\nline" produced This\ncontains\nline It is not reasonable to expect the shell's handling of escape characters to match any particular languages handling of escape characters. For example, Java and C have different sets of escape characters; C includes two-character escapes Java omitts and Java has Unicode escapes. On a system with both C and Java, which convention should be used? Closing as not a bug. ###@###.### 2003-11-05
05-11-2003