Maven局部变量“需要声明为final”,其中不存在变量或变量已经为final

时间:2018-11-09 02:08:24

标签: java eclipse maven

我正在尝试以目标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

根据我的研究,通常的解决方案是将变量声明为final,但是第71行没有变量,而89行的变量已经是final ,如{ {1}},插入下面。

SetupWindow.java

第71行是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."; } } ,第89行是gbc_header.anchor = GridBagConstraints.NORTH;

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

如果您仔细查看错误

  

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

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

您在第89行中将module用作module.getDir()

另外,行号是89,行号是71。

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