将main()添加到Java类的问题

时间:2013-06-26 02:24:10

标签: java frameworks compiler-errors

Newb to Java,似乎无法在Google上找到这个简单问题的正确答案。

我正在使用遗传编程框架进行一些研究,并且一直在尝试根据我的需求定制他们的示例。定义主要功能留给用户,这就是我想要做的。

我正在尝试创建一个可运行的文件。它扩展了一个更高级别的类来调用其他功能。

以下是我遇到问题的代码(来自内置示例):

public class Multiplexer extends GPModel {
    public Multiplexer(final int noInputBits) {
      ...
    }

    ...
    public static void main(String[] args) {
        new Multiplexer(6).run();
    }  
}

这是我看到的错误。我正在使用带有JDK 1.7的NetBeans 7.3.1。

  

错误:在类org.epochx.gp.model.Multiplexer中找不到主方法,   请将main方法定义为:public static void main(String []   args)Java结果:1 BUILD SUCCESSFUL(总时间:0秒)

它应该是相当微不足道的....我已经创建了具有main函数的其他类并且没有遇到这个问题...它似乎只是在框架中定义的预先存在的类中这个问题。


修改

抱歉,封装类已公开,忘了提及。


Edit2 完整的来源,根据要求...(epochx.org,如果有人有兴趣,v1.4.1)

package org.epochx.gp.model;

import java.util.*;

import org.epochx.epox.*;
import org.epochx.epox.bool.*;
import org.epochx.epox.lang.IfFunction;
import org.epochx.gp.representation.GPCandidateProgram;
import org.epochx.representation.CandidateProgram;
import org.epochx.tools.util.BoolUtils;

/**
 * GP model for the multiplexer problems.
 * 
 * <h4>Multiplexer problem</h4>
 * 
 * Given n binary inputValues, a program that solves the majority problem will
 * return true in all circumstances where a majority of the inputValues are true
 * (or 1), and return false whenever there is not a majority of true values.
 */
public class Multiplexer extends GPModel {

    // The names of the inputValues used in the grammar.
    private final Variable[] variables;

    // The boolean input sequences.
    private final boolean[][] inputValues;

    // No input bits.
    private int noAddressBits;
    private int noDataBits;

    /**
     * Constructs a Multiplexer model for the given number of inputs.
     * 
     * @param noInputBits the number of inputs the multiplexer problem should be
     *        for
     */
    public Multiplexer(final int noInputBits) {
        // Generate the input sequences.
        inputValues = BoolUtils.generateBoolSequences(noInputBits);

        // Calculate number of address/data bits.
        setBitSizes(noInputBits);

        // Define functions.
        final List<Node> syntax = new ArrayList<Node>();
        syntax.add(new IfFunction());
        syntax.add(new AndFunction());
        syntax.add(new OrFunction());
        syntax.add(new NotFunction());

        // Define terminal variables.
        variables = new Variable[noInputBits];
        // Add address inputs.
        for (int i = 0; i < noAddressBits; i++) {
            variables[i] = new Variable("a" + i, Boolean.class);
            syntax.add(variables[i]);
        }
        // Add data inputs.
        for (int i = noAddressBits; i < noInputBits; i++) {
            variables[i] = new Variable("d" + i, Boolean.class);
            syntax.add(variables[i]);
        }

        setSyntax(syntax);
    }

    /**
     * Calculates the fitness score for the given program. The fitness of a
     * program for the majority problem is calculated by evaluating it
     * using each of the possible sets of input values. There are
     * <code>2^noInputBits</code> possible sets of inputs. The fitness of the
     * program is the quantity of those input sequences that the program
     * returned an incorrect response for. That is, a fitness value of
     * <code>0.0</code> indicates the program responded correctly for every
     * possible set of input values.
     * 
     * @param p {@inheritDoc}
     * @return the calculated fitness for the given program.
     */
    @Override
    public double getFitness(final CandidateProgram p) {
        final GPCandidateProgram program = (GPCandidateProgram) p;

        double score = 0;

        // Execute on all possible inputs.
        for (final boolean[] in: inputValues) {

            // Set the variables.
            for (int i = 0; i < in.length; i++) {
                variables[i].setValue(in[i]);
            }

            if ((Boolean) program.evaluate() == multiplex(in)) {
                score++;
            }
        }

        return inputValues.length - score;
    }

    /*
     * Calculate and set the number of address and data bits.
     */
    private void setBitSizes(final int noInputBits) {
        noAddressBits = 1;
        while (true) {
            noDataBits = (int) Math.pow(2, noAddressBits);

            if ((noAddressBits + noDataBits) == noInputBits) {
                break;
            }

            noAddressBits++;
        }
    }

    /*
     * Calculate what the correct response should be for the given inputs.
     */
    private Boolean multiplex(final boolean[] vars) {
        // Calculate which data position to use.
        int dataPosition = 0;
        for (int i = 0; i < noAddressBits; i++) {
            if (vars[i]) {
                dataPosition += Math.pow(2, i);
            }
        }

        return vars[noAddressBits + dataPosition];
    }

    @Override
    public Class<?> getReturnType() {
        return Boolean.class;
    }


  public static void main(String[] args) {
        new Multiplexer(6).run();
    }  
}

4 个答案:

答案 0 :(得分:1)

您的IDE可能仍在引用一些旧的jar或类文件。做一个干净/构建/刷新(我不使用NetBeans,但无论等价物是什么),你的更改应该注册。

答案 1 :(得分:1)

<强>更新

我想我已经明白了!

您的源代码在Multiplexer中定义了一个名为org.epochx.gp.model的类。 But there is already a class called Multiplexer in org.epochx.gp.model

所以最有可能发生的是你的构建类路径和你的启动类路径是不同的,在启动的情况下,IDE的启动器首先找到标准的Multiplexer ......那个人根本就没有main方法。

  

第1课:不要在其他人的包中声明你的课程!

     

第1a课:不要在其他人的代码中进行更改......除非你必须这样做。 (在这种情况下,你没有。)


(原始答案......)

我认为问题是您的Multiplexer课程不是public。虽然java命令不需要这个,但看起来就像问题是IDE本身无法识别类中的入口点......

如果不是这样,那么另一种可能性是您的IDE与文件系统(或类似的东西)不同步并且需要重新启动。

另一种可能性是源代码中的String指的是java.lang.String以外的其他内容。但除非你使用一些新版本的org.epochx来定义自己的String类,否则我认为这是不可能的。 (并且网络上的org.epochx javadocs不包含String类。)

我能想到的另一个可能性是“时髦的角色”。也许您的main标识符中的一个字符不是罗马/拉丁字母。某些Unicode字符看起来像拉丁字母......但不是。有更多信息here(虽然在不同的背景下......)。


如果所有其他方法都失败了,请尝试使用java命令从命令行运行您的应用程序,看看是否有任何区别。如果没有,请尝试使用javap检查“.class”文件,找出main方法真正的签名。

答案 2 :(得分:1)

也许尝试将您的课程(Multiplexer)公开。

答案 3 :(得分:1)

我尝试了你的代码并为我工作..尝试清理和构建...或者更容易创建一个新类测试并将主要部分放在那里。