Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get Javac to work on Mac OS X

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.

like image 780
user331570 Avatar asked Jan 18 '26 00:01

user331570


2 Answers

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.

like image 195
Jon Skeet Avatar answered Jan 20 '26 13:01

Jon Skeet


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

like image 31
trashgod Avatar answered Jan 20 '26 13:01

trashgod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!