Java 标记“;”上的语法错误,{ 预期在此标记之后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1755259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:20:46  来源:igfitidea点击:

Syntax error on token ";", { expected after this token

javaeclipse

提问by silverkid

why is there syntax error on this line ( shown below )

为什么这一行有语法错误(如下所示)

package org.temp2.cod1;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;

public class Code1 {

    byte[] plaintext = new byte[32];   // <<<<<<<<<<<<<<<<<<<<<<<<<< syntax error
    for (int i = 0; i < 32; i++) {
      plaintext[i] = (byte) (i % 16);
    }

    byte[] key = new byte[16];
    SecureRandom r = new SecureRandom();
    r.nextBytes(key);

    Cipher c = Cipher.getInstance("AES");
    SecretKeySpec k =  new SecretKeySpec(key, "AES");
    c.init(Cipher.ENCRYPT_MODE, k);
    byte[] encryptedData = c.doFinal(plaintext);
}
}

采纳答案by Emil H

You forgot the entry point method declaration. Try adding:

您忘记了入口点方法声明。尝试添加:

public static void main(String[] args) {

before the line where you got the error.

在出现错误的行之前。

回答by Bozho

your code should be inside a method. It appears to me that you have skipped the public void method(..) {line

你的代码应该在一个方法中。在我看来你跳过了这public void method(..) {条线