簡體   English   中英

Eclipse中的ClassNotFoundException,所有包都在同一個src文件夾中

[英]ClassNotFoundException in Eclipse, all packages are in the same src folder

我正在從控制台運行的計算器程序上工作,該程序也應該支持創建內聯函數,例如inline _FOO{a1, a2} a1 + a2聲明了該函數並調用_FOO{2,3}應該返回2+ 3。 我正在使用Converter類進行所有解析和計算。 我在同一包中創建了一個名為InlineFunction的類,該類保存程序默認函數的實現,這些函數都可以正常工作,但是在嘗試調用內聯函數時出現錯誤。 調試時,我注意到程序可以正常運行,直到我實際聲明新Inline Function的那部分為止, InlineFunction function = new InlineFunction(); 我立即收到ClassNotFoundException。 我不知道為什么,因為那個函數和另一個起作用的函數在同一個包中,所以我將整個包導入Converter類中。

編輯:InlineFunction的代碼:

package oop.ex2.functions;

import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import oop.ex2.main.UndeclaredVariableException;

public class InlineFunction implements Calculable {
    //Regular expression for finding a variable name.
    private static final String VARIABLE_NAME_REGEX = "@[a-z0-9]+";
    //Pattern for finding a variable name.
    private static final Pattern VARIABLE_NAME = Pattern.compile(VARIABLE_NAME_REGEX);
    private LinkedHashMap<String, Double> _parameters;
    private String _code;

    public InlineFunction(String code, String[] parameters) {
        _parameters = new LinkedHashMap<String, Double>();
        _code = code;
        for (String name: parameters) {
            _parameters.put(name, 0d);
        }
    }

    @Override
    public String calculate(Double[] parameters) {
        if (parameters.length != _parameters.size()) {
            throw new IllegalParameterNumberException();
        }
        int parameterIndex = 0;
        for (String key: _parameters.keySet()){
            _parameters.put(key, parameters[parameterIndex]);
        }
        _code = replaceParameters(_code);
        return _code;
    }

    private String replaceParameters(String expression) {
        Matcher variableName = VARIABLE_NAME.matcher(expression);
        while (variableName.find()) {
            if (_parameters.containsKey(variableName.group())) {
                expression = variableName.replaceFirst(_parameters.get(variableName.group()).toString());
                //Reset the matcher, since the expression was changed
                variableName = VARIABLE_NAME.matcher(expression);
            } else {
                throw new UndeclaredVariableException();
            }
        }
        return expression;
    }
}

該問題是由於未正確處理提供給構造函數的輸入而已得到修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM