Tuesday, September 19, 2006

Java compile error because of text encoding

I have the magical ability to find every possible error in the course of learning about something. Take for example todays exploits. I'm developing on windows XP using Notepad++.

I'm reading "Java For Artists—The Art, Philosophy, and Science of Object-Oriented Programming" and given the simple code below:

-----------------------------------------------------------------------------------------------
Example 2.1: SampleClass.java

package com.pulpfreepress.jfa.chapter1;

import java.util.*;

public class SampleClass {
/**************************************
Class and instance field declarations
***************************************/
public static final int CONST_VAL = 25;
private static int class_variable = 0;
private int instance_variable = 0;

public SampleClass(){
System.out.println("Sample Class Lives!");
}

public static void setClassVariable(int val){
class_variable = val;
}

public static int getClassVariable(){
return class_variable;
}

public void setInstanceVariable(int val){
instance_variable = val;
}

public int getInstanceVariable(){
return instance_variable;
}
}


-----------------------------------------------------------------------------------------------
Example 2.2: ApplicationClass.java

package com.pulpfreepress.jfa.chapter1;

public class ApplicationClass {

public static void main(String args[]){
SampleClass sc = new SampleClass();
System.out.println(SampleClass.CONST_VAL);
System.out.println(SampleClass.getClassVariable());
System.out.println(sc.getInstanceVariable());
SampleClass.setClassVariable(3);
sc.setInstanceVariable(4);
System.out.println(SampleClass.getClassVariable());
System.out.println(sc.getInstanceVariable());

System.out.println(sc.getClassVariable());
}
}
-----------------------------------------------------------------------------------------------

I still got compile errors as such:
[parsing started src\com\pulpfreepress\jfa\chapter1\ApplicationClass.java]
src\com\pulpfreepress\jfa\chapter1\ApplicationClass.java:1: 'class' or 'interface' expected
package com.pulpfreepress.jfa.chapter1;
^
src\com\pulpfreepress\jfa\chapter1\ApplicationClass.java:1: illegal character: \187
package com.pulpfreepress.jfa.chapter1;
^
src\com\pulpfreepress\jfa\chapter1\ApplicationClass.java:1: illegal character: \191
package com.pulpfreepress.jfa.chapter1;
^
[parsing completed 62ms]
[parsing started src\com\pulpfreepress\jfa\chapter1\SampleClass.java]
src\com\pulpfreepress\jfa\chapter1\SampleClass.java:1: 'class' or 'interface' expected
package com.pulpfreepress.jfa.chapter1;
^
src\com\pulpfreepress\jfa\chapter1\SampleClass.java:1: illegal character: \187
package com.pulpfreepress.jfa.chapter1;
^
src\com\pulpfreepress\jfa\chapter1\SampleClass.java:1: illegal character: \191
package com.pulpfreepress.jfa.chapter1;
^
src\com\pulpfreepress\jfa\chapter1\SampleClass.java:2: 'class' or 'interface' expected
import java.util.*;
^
[parsing completed 31ms]
[total 93ms]
7 errors

After hours and several Google searches on 'class' or 'interface' expected, checking syntax, etc with no success, I wondered what the hell this: was.

Turns out, in my efforts to create files that would be usable in Unix and Windows I encoded my text files as UTF-8. Upon changing them back to Ansii they compiled without error.

Time to read up on Unicode.

Labels: ,

1 Comments:

Anonymous Anonymous said...

Thank you so much, I don't remember when I changed the format, but this error makes me crazy.

Monday, October 13, 2008 5:05:00 PM  

Post a Comment

<< Home