为什么我不能在CMD上编译这个简单的Java代码,而是在在线编译器上编译它?

时间:2015-10-01 17:10:23

标签: java cmd compilation

我有一本书中的Java代码(名为Schaum的计算机科学原理概要):

public class Main{
    public static void main(String[]args){
        int x;
        int y;
        int z;

        y=7;
        z=4;
        x=y+z;
        System.out.println("x = "+x);
    }
}

我已经改变了#34; Primitive"到" Main"。它可以在这个网站上编译:

http://www.learnjavaonline.org/

输出只是" x = 11"。

但是当尝试使用javac Main.java在Windows 8.1上使用CMD编译它时,我收到此错误消息(文件名为Main.java):

Main.java:1: error: illegal character: '\u0000'
 ■p u b l i c   c l a s s   M a i n {
               ^
Main.java:1: error: illegal character: '\u0000'
 ■p u b l i c   c l a s s   M a i n {
                           ^
Main.java:1: error: <identifier> expected
 ■p u b l i c   c l a s s   M a i n {
                                    ^
Main.java:1: error: illegal character: '\u0000'
 ■p u b l i c   c l a s s   M a i n {
                                     ^
Main.java:2: error: illegal character: '\u0000'

^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
              ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                            ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                      ^
Main.java:3: error: illegal start of type
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                               ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                              ^
Main.java:3: error: ';' expected
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                               ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                                ^
Main.java:3: error: illegal start of type
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                                         ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                                          ^
Main.java:3: error: illegal character: '\u0000'
 p u b l i c   s t a t i c   v o i d   m a i n ( S t r i n g [ ] a r g s ) {
                                                                            ^
Main.java:4: error: illegal character: '\u0000'

^
Main.java:5: error: illegal character: '\u0000'
 i n t   x ;
^
Main.java:5: error: illegal character: '\u0000'
 i n t   x ;
        ^
Main.java:5: error: not a statement
 i n t   x ;
 ^
Main.java:5: error: not a statement
 i n t   x ;
         ^
Main.java:5: error: illegal character: '\u0000'
 i n t   x ;
            ^
Main.java:6: error: illegal character: '\u0000'

^
Main.java:7: error: illegal character: '\u0000'
 i n t   y ;
^
Main.java:7: error: illegal character: '\u0000'
 i n t   y ;
        ^
Main.java:7: error: not a statement
 i n t   y ;
 ^
Main.java:7: error: not a statement
 i n t   y ;
         ^
Main.java:7: error: illegal character: '\u0000'
 i n t   y ;
            ^
Main.java:8: error: illegal character: '\u0000'

^
Main.java:9: error: illegal character: '\u0000'
 i n t   z ;
^
Main.java:9: error: illegal character: '\u0000'
 i n t   z ;
        ^
Main.java:9: error: not a statement
 i n t   z ;
 ^
Main.java:9: error: not a statement
 i n t   z ;
         ^
Main.java:9: error: illegal character: '\u0000'
 i n t   z ;
            ^
Main.java:10: error: illegal character: '\u0000'

^
Main.java:11: error: illegal character: '\u0000'

^
Main.java:12: error: illegal character: '\u0000'

^
Main.java:13: error: illegal character: '\u0000'
 y = 7 ;
^
Main.java:13: error: illegal character: '\u0000'
 y = 7 ;
    ^
Main.java:13: error: illegal character: '\u0000'
 y = 7 ;
      ^
Main.java:13: error: not a statement
 y = 7 ;
     ^
Main.java:13: error: illegal character: '\u0000'
 y = 7 ;
        ^
Main.java:14: error: illegal character: '\u0000'

^
Main.java:15: error: illegal character: '\u0000'
 z = 4 ;
^
Main.java:15: error: illegal character: '\u0000'
 z = 4 ;
    ^
Main.java:15: error: illegal character: '\u0000'
 z = 4 ;
      ^
Main.java:15: error: not a statement
 z = 4 ;
     ^
Main.java:15: error: illegal character: '\u0000'
 z = 4 ;
        ^
Main.java:16: error: illegal character: '\u0000'

^
Main.java:17: error: illegal character: '\u0000'
 x = y + z ;
^
Main.java:17: error: illegal character: '\u0000'
 x = y + z ;
    ^
Main.java:17: error: illegal character: '\u0000'
 x = y + z ;
        ^
Main.java:17: error: not a statement
 x = y + z ;
       ^
Main.java:17: error: not a statement
 x = y + z ;
         ^
Main.java:17: error: illegal character: '\u0000'
 x = y + z ;
            ^
Main.java:18: error: illegal character: '\u0000'

^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
              ^
Main.java:19: error: not a statement
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
             ^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                      ^
Main.java:19: error: not a statement
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                     ^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                      ^
Main.java:19: error: ';' expected
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                       ^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                  ^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                    ^
Main.java:19: error: not a statement
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                   ^
Main.java:19: error: not a statement
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                     ^
Main.java:19: error: ';' expected
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                       ^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                        ^
Main.java:19: error: illegal character: '\u0000'
 S y s t e m . o u t . p r i n t l n ( " x   =   " + x ) ;
                                                          ^
Main.java:20: error: illegal character: '\u0000'

^
Main.java:21: error: illegal character: '\u0000'
 }
^
Main.java:21: error: illegal character: '\u0000'
 }
  ^
Main.java:22: error: illegal character: '\u0000'

^
Main.java:23: error: illegal character: '\u0000'
 }
^
Main.java:23: error: illegal character: '\u0000'
 }
  ^
Main.java:23: error: reached end of file while parsing
 }
   ^
78 errors

即使删除所有空格,我也会收到35个错误。

我安装的Java版本是: Java 8 Update 60(64位)(我有Windows 64位) Java SE开发工具包8更新60(64位) 两个版本都是8.0.600.27

为什么我无法在CMD上编译?是不是因为我使用的是Notepad 6.3(Windows 8.1和其他版本中包含的标准记事本)?

1 个答案:

答案 0 :(得分:3)

您的文件保存为UTF-16,但您没有告诉编译器(-encoding)。

建议您在记事本中打开该文件,并将其再次保存在系统的默认代码页中。