views:

208

answers:

2

I am trying to compile with javac on Snow Leopard through the command line. I have Xcode installed. I am just using a simple Hello World file, it works in Eclipse but I can't get it to work using javac.
javac -version returns javac 1.6.0_17

HelloWorld.java

public class HelloWorld
{
   public static void main(String[] args)
   {
 String message = "Welcome to Java!";
        System.out.println(message);
   }
}

I type: javac HelloWorld.java

and get the following error.

HelloWorld.java:1: class, interface, or enum expected
public class HelloWorld 
^
1 error

and...

javac -cp . HelloWorld.java

returns the same.

echo $CLASSPATH just returns blank.

Thanks for the help.

+8  A: 

Are you using UTF-8 perhaps, with a byte order mark at the start of the file? Perhaps that's confusing javac?

Have a look at the file with a hex editor to see what it looks like.

Jon Skeet
It is UTF-8, what should I change it to?
ok, I changed it to Wester (Mac OS Roman) and it worked! Thank you, but what if I do want to use UTF-8? How would I do that?
You could leave it at UTF-8, and run `javac -encoding UTF-8 ...` Otherwise, it uses the platform default.
Chris Lercher
@elguapo-85: I would personally follow chris_l's recommendation of specifying it explicitly on the command line: UTF-8 is a generally good choice for text files.
Jon Skeet
+2  A: 

On Mac OS X 10.5.8, the default file.encoding is MacRoman. Eclipse uses this as its default, but that default may be changed in Eclipse > Preferences > General > Workspace > Text file encoding, as well as in each project's properties. In NetBeans, a similar setting is available in each project's properties. EclipseWorkspace

trashgod
Thank you very much. Is it possible to change the default encoding of Mac OS X or will this cause problems?
trashgod