如何通过编译Java文件修复错误

时间:2019-06-15 23:13:08

标签: java compiler-errors

我怎么了?

我试图编译在互联网上找到的Java代码:

javac 1.java

handleClickButton = (event,some-unique-id) => {
...
this.setState({
  open: {
      [some-unique-id]: true
  }
});
};

我有错误:

public static void copy(File src, File dst) throws IOException {
   InputStream in = new FileInputStream(src);
   try {
       OutputStream out = new FileOutputStream(dst);
       try {
           // Transfer bytes from in to out
           byte[] buf = new byte[1024];
           int len;
           while ((len = in.read(buf)) > 0) {
               out.write(buf, 0, len);
           }
       } finally {
           out.close();
       }
   } finally {
       in.close();
   }

如何解决这些错误?

更新。我所做的:

javac '/1.java' 
---------
 ERROR in 1.java (at line 1)
    public static void copy(File src, File dst) throws IOException {
                  ^^^^
syntax error on token "void", @ expected
---------
 ERROR in 1.java (at line 1)
    public static void copy(File src, File dst) throws IOException {
                            ^^^^
syntax error on token "File", = expected after this token
---------
 ERROR in 1.java (at line 1)
    public static void copy(File src, File dst) throws IOException {
                                      ^^^^
syntax error on token "File", = expected after this token
---------
 ERROR in 1.java (at line 1)
    public static void copy(File src, File dst) throws IOException {
                                                ^^^^^^
syntax error on token "throws", interface expected
---------
 ERROR in 1.java (at line 3)
    try {
    ^^^
syntax error on token "try", delete this token
---------
 ERROR in 1.java (at line 15)
    } finally {
      ^^^^^^^
syntax error on token "finally", delete this token
---------
 problems (6 errors)

在编译(import java.io.*; //import java.awt.*; //import java.awt.event.*; //import javax.swing.*; //import java.util.*; //import java.text.*; //import java.util.regex.*; class Copy { public final String dst = "/home/ubuntu/1/1"; public final String src = "/home/ubuntu/assets/ipt.txt"; public static void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); try { OutputStream out = new FileOutputStream(dst); try { // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { out.close(); } } finally { in.close(); } } } )和执行(javac 1.java)时,出现此错误:

java Copy

1 个答案:

答案 0 :(得分:0)

  1. JVM将首先在运行时搜索执行程序的主要方法,因此请声明 主要方法
  2. 由于您已将复制方法声明为静态方法,因此需要更改 实例变量设置为静态。

下面是将其签出的代码:

    import java.io.*;
   class Copy {
   public final static String dst = "/home/ubuntu/1/1";
   public final static String src = "/home/ubuntu/assets/ipt.txt";
   static File fileSrc=new File(src);
   static File fileDst=new File(dst);
   public static void main(String[] args) throws IOException {
       System.out.println("main");
       Copy.copy(fileSrc,fileDst);
   }
   public static void copy(File src, File dst) throws IOException {
      InputStream in = new FileInputStream(src);
      try {
         OutputStream out = new FileOutputStream(dst);
         try {
            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
               out.write(buf, 0, len);
            }
         } finally {
            out.close();
         }
      } finally {
         in.close();
      }
   }
}