Maven 局部变量"needs to be declared final"不存在变量或变量已经是最终变量的地方



我正试图以clean package shade:shade为目标编译我的程序;然而,我遇到了这个问题:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project core: Compilation failure
[ERROR] /C:/Users/justi/Programming/YAML to Bot/YamlToBot/core/src/main/java/com/justinschaaf/yamltobot/core/setup/SetupWindow.java:[89,71] local variable module is accessed from within inner class; needs to be declared final

根据我的研究,通常的解决方案是将变量声明为最终变量,但第71行没有变量,第89行的变量已经是最终变量,如下面插入的SetupWindow.java所示。

package com.justinschaaf.yamltobot.core.setup;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import com.justinschaaf.yamltobot.core.common.Module;
import com.justinschaaf.yamltobot.core.common.Reference;
import com.justinschaaf.yamltobot.core.common.VersionChecker;
import com.justinschaaf.yamltobot.core.handler.ConfigHandler;
/**
* 
* The primary class for setting up the window for YamlToBot
* 
* @author Justin Schaaf
* @since 1.0.0
*
*/
public class SetupWindow extends JFrame {
private JPanel contentPane;
/**
* The primary function for setting up the window
* @since 1.0.0
*/
public SetupWindow(Module module) {
setTitle("YamlToBot | " + ConfigHandler.getString("name", module.getName()));
setIconImage(Toolkit.getDefaultToolkit().getImage(SetupWindow.class.getResource("/assets/icon.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
JPanel header = new JPanel();
FlowLayout flowLayout = (FlowLayout) header.getLayout();
flowLayout.setVgap(0);
flowLayout.setHgap(0);
header.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
header.setBackground(new Color(Integer.parseInt("283F50",16)));
GridBagConstraints gbc_header = new GridBagConstraints();
gbc_header.anchor = GridBagConstraints.NORTH;
gbc_header.gridheight = 2;
gbc_header.fill = GridBagConstraints.HORIZONTAL;
gbc_header.gridx = 0;
gbc_header.gridy = 0;
contentPane.add(header, gbc_header);
JLabel label = new JLabel("");
label.setIcon(new ImageIcon(SetupWindow.class.getResource("/assets/logo.png")));
header.add(label);
final JTextArea log = new JTextArea();
log.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent arg0) {
try {
final File logFile = new File(module.getDir() + "logs/log-latest.log");
BufferedReader logText = new BufferedReader(new InputStreamReader(new FileInputStream(logFile)));
StringBuilder text = new StringBuilder();
String line;
while ((line = logText.readLine()) != null) {
text.append(line + "n");
}
logText.close();
log.setText(text.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
log.setFont(new Font("Monospaced", Font.PLAIN, 18));
log.setEditable(false);
log.setText("Move the cursor here to update the log!");
GridBagConstraints gbc_log = new GridBagConstraints();
gbc_log.gridheight = 3;
gbc_log.fill = GridBagConstraints.BOTH;
gbc_log.gridx = 0;
gbc_log.gridy = 2;
contentPane.add(log, gbc_log);
JPanel footer = new JPanel();
FlowLayout flowLayout_1 = (FlowLayout) footer.getLayout();
flowLayout_1.setVgap(0);
flowLayout_1.setHgap(0);
footer.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
footer.setBackground(new Color(Integer.parseInt("8693A4",16)));
GridBagConstraints gbc_footer = new GridBagConstraints();
gbc_footer.anchor = GridBagConstraints.SOUTH;
gbc_footer.fill = GridBagConstraints.HORIZONTAL;
gbc_footer.gridx = 0;
gbc_footer.gridy = 5;
contentPane.add(footer, gbc_footer);
final JTextPane version = new JTextPane();
version.setEditable(false);
version.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
version.setBackground(new Color(Integer.parseInt("8693A4",16)));
version.setFont(new Font("Tahoma", Font.BOLD, 15));
version.setText(Reference.version);
footer.add(version);
final JTextPane update = new JTextPane();
update.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (VersionChecker.isUpdated() == false) {
try {
Desktop.getDesktop().browse(new URL(Reference.releasesURL).toURI());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
});
update.setEditable(false);
update.setForeground(new Color(Integer.parseInt("FEFEFE",16)));
update.setBackground(new Color(Integer.parseInt("8693A4",16)));
update.setFont(new Font("Tahoma", Font.PLAIN, 18));
update.setText(updateText());
footer.add(update);
}
/**
* Checks if the jar is updated and returns text accordingly
* @return The text stating whether or not the jar is up to date
* @since 1.0.0
*/
private String updateText() {
if (VersionChecker.isUpdated() == false) return "A new update is available: " + VersionChecker.getLatestVersion();
if (Reference.prerelease == true) return "You are using a pre-release build.";
else return "Your jar is up to date.";
}
}   

第71行为gbc_header.anchor = GridBagConstraints.NORTH;,第89行为final File logFile = new File(module.getDir() + "logs/log-latest.log");

提前感谢您抽出时间。

如果您仔细查看错误

SetupWindow.java:从内部访问[89,71]局部变量模块内部阶级;需要宣布为最终

它告诉名为module的变量不是最终变量。不能使用在匿名内部类外部、内部类内部声明的非最终局部变量。

您正在使用第89行中的module作为module.getDir()

另外,89是行号,71是该行中的列或字符号。

要修复此问题,请将public SetupWindow(Module module)更改为public SetupWindow(final Module module)

最新更新