1

I am attempting to compile my program with the goals clean package shade:shade; however, I run into this issue:

[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

From my research, the usual solution is to declare the variable as final, but there is not a variable at line 71 and the variable at line 89 is already final, as can be seen by SetupWindow.java, inserted below.

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.";

    }

}   

Line 71 is gbc_header.anchor = GridBagConstraints.NORTH;, and line 89 is final File logFile = new File(module.getDir() + "logs/log-latest.log");

Thank you in advance for your time.

1 Answer 1

0

If you look closely at the error

SetupWindow.java:[89,71] local variable module is accessed from within inner class; needs to be declared final

It tells that the variable named module is not final. You can't use a non-final local variable that is declared outside the anonymous inner class, inside that inner class.

You are using module in line 89 as module.getDir().

Also, 89 is the line number, 71 is the column or character number in that line.

To fix it, change public SetupWindow(Module module) to public SetupWindow(final Module module).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.