Java 编码 Base64 找不到符号错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39711122/
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
Encode Base64 cannot find symbol error
提问by Shiv Patel
It gives me a error when I try to compile it in terminal. It prints out this error:
当我尝试在终端中编译它时,它给了我一个错误。它打印出这个错误:
-bash-4.1$ javac CPS3498/HW_Ch2/encrypt.java
CPS3498/HW_Ch2/encrypt.java:9: cannot find symbol
symbol : class Base64
location: package java.util
import java.util.Base64;
^
CPS3498/HW_Ch2/encrypt.java:61: cannot find symbol
symbol : variable Base64
location: class encrypt
String encryptedValue = Base64.getEncoder().encodeToString(encVal);
^
2 errors
I completely lost on how to fix this problem. I have tried different java utilities to compile and they all give me almost the same error.
我完全不知道如何解决这个问题。我尝试了不同的 Java 实用程序进行编译,它们都给了我几乎相同的错误。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
//create public class encrypt
public class encrypt {
//algorithm AES 128 with a secret key
private static final String ALGO = "AES";
private static final byte[] keyValue =
new byte[] { 'H', 't', 'v', 'b', 'a', 'w', 'e',
'i', 'n', 'v', 'a','l', 't', 'k', 'y', 'e' };
private static BufferedReader reader;
public static void main(String[] args) throws Exception {
//Filereader letter to read from a file letter.txt
FileReader letter = new FileReader("/Users/Shiv/Eclipse/CPS3498_HW/src/letter.txt");
reader = new BufferedReader(letter);
//string text blank, data that stres reader contents.
String text = "";
String data = reader.readLine();
//while loop to see if data is not blank
while (data != null){
text += data;
data = reader.readLine();
}
String textEnc = encrypt(text);
//
File secret = new File("/Users/Shiv/Eclipse/CPS3498_HW/src/secret.txt");
try
{
secret.createNewFile();
}
catch(Exception e)
{
e.printStackTrace();
}
try {
FileWriter secretFile = new FileWriter(secret);
BufferedWriter secretBuff = new BufferedWriter(secretFile);
secretBuff.write(textEnc);
secretBuff.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//encrypt method
public static String encrypt(String Data) throws Exception {
Key pass = generateKey();
// cipher class to provide the encryption and intialize
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, pass);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = Base64.getEncoder().encodeToString(encVal);
return encryptedValue;
}
//generateKey method to generate a secret key
private static Key generateKey() throws Exception {
Key pass = new SecretKeySpec(keyValue, ALGO);
return pass;
}
}
回答by Magnilex
java.util.Base64
is available since Java 8. You are compiling the class with an older Java version. javac -versionwill show you which one you are using.
java.util.Base64
从 Java 8 开始可用。您正在使用较旧的 Java 版本编译该类。javac -version将显示您正在使用哪一个。
回答by uday
src: Which Java library provides base64 encoding/decoding?
Java 6 and 7
Java 6 和 7
Since Java 6 you can use the lesser known class javax.xml.bind.DatatypeConverter. This is part of the JRE, no extra libraries required.
从 Java 6 开始,您可以使用鲜为人知的类 javax.xml.bind.DatatypeConverter。这是 JRE 的一部分,不需要额外的库。
byte[] message = "hello world".getBytes("UTF-8");
String encoded = DatatypeConverter.printBase64Binary(message);
byte[] decoded = DatatypeConverter.parseBase64Binary(encoded);
System.out.println(encoded);
System.out.println(new String(decoded, "UTF-8"));
Output
输出
aGVsbG8gd29ybGQ= hello world
aGVsbG8gd29ybGQ=你好世界
回答by Carlos ABS
I had the same problem, the solution to me was just running IntelliJ as root:
我遇到了同样的问题,我的解决方案只是以 root 身份运行 IntelliJ:
sudo ./idea.sh
须藤./idea.sh
回答by Manzn
I had the same problem despite running on jdk1.8.0_181.
尽管在 jdk1.8.0_181 上运行,但我遇到了同样的问题。
I got it to work with following code:
我让它使用以下代码:
import java.util.Base64;
...
byte[] encodedPv = Base64.getDecoder().decode(PRIVATE_KEY);
...
Hope this helps someone! Cheers Manzn
希望这可以帮助某人!干杯曼兹恩
回答by Yuri
This is indeed a problem of using an older Java SDK than version 8.
这确实是使用比版本 8 更旧的 Java SDK 的问题。
At some OSes it can be tricky to determine which version of compiler/SDK is actually used.
在某些操作系统上,确定实际使用的是哪个版本的编译器/SDK 可能很棘手。
It is possible to get currently configured compiler version by issuing:
可以通过发出以下命令来获取当前配置的编译器版本:
javac -version
This should return version 1.8 or newer.
这应该返回 1.8 或更高版本。
//edit: The java.util.Base64
is available in JDK 8 and newer. So it really helps to determine the used version of javac
.
//编辑:java.util.Base64
在 JDK 8 和更新版本中可用。因此,它确实有助于确定javac
.