Java – token “;” Syntax error on, {expected after this tag

Why is there a syntax error in this line (as 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);
}
}

Solution

You forgot the entry point method declaration Try adding:

public static void main(String[] args) {

Before you receive the wrong line

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>