Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Maven:未指定源。。。但是为什么呢?_Java_Eclipse_Maven_Jar - Fatal编程技术网

Java Maven:未指定源。。。但是为什么呢?

Java Maven:未指定源。。。但是为什么呢?,java,eclipse,maven,jar,Java,Eclipse,Maven,Jar,好的,这是我的第一个Maven项目。这是我需要完成的最后一件事,但它一直指向我 java.lang.IllegalStateException: source not specified at org.eclipse.jdt.core.dom.ASTParser.createAST (ASTParser.java:830) at com.javaAST.ASTParse.parse (ASTParse.java:25) at com.javaAST.Main.main (

好的,这是我的第一个Maven项目。这是我需要完成的最后一件事,但它一直指向我

java.lang.IllegalStateException: source not specified
    at org.eclipse.jdt.core.dom.ASTParser.createAST (ASTParser.java:830)
    at com.javaAST.ASTParse.parse (ASTParse.java:25)
    at com.javaAST.Main.main (Main.java:59)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:567)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
    at java.lang.Thread.run (Thread.java:830)

归根结底,这是从这一页

package com.javaAST;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;

public class ASTParse
{
    public static FileList fileList = new FileList();
    public static ASTParser parser = ASTParser.newParser(AST.JLS3);

    public static void parse(List<String> results) throws IOException
    {

        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

        for (String result : results)
        {
            //Create output file
            String addExtension = result.toLowerCase() + ".ast";
            System.out.println("Creating File: " + addExtension);
            File output = new File(addExtension);
            output.createNewFile();
            FileWriter fw = new FileWriter(output);


            //Begin ASTParser
            parser.setSource(result.toCharArray());
            parser.setKind(ASTParser.K_COMPILATION_UNIT);

            cu.accept(new ASTVisitor() {

                Set<String> names = new HashSet<>();

                public boolean visit(VariableDeclarationFragment node) {
                    SimpleName name = node.getName();
                    this.names.add(name.getIdentifier());
                    try
                    {
                        fw.write("Declaration of '" + name + "' at line" + cu.getLineNumber(name.getStartPosition()) + "\n");
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    return false; // do not continue to avoid usage info
                }

                public boolean visit(SimpleName node) {
                    if (this.names.contains(node.getIdentifier())) {
                        try
                        {
                            fw.write("Usage of '" + node + "' at line " + cu.getLineNumber(node.getStartPosition()) + "\n");
                        } catch (IOException e)
                        {
                            e.printStackTrace();
                        }
                    }
                    return true;
                }
            });
        }
    }
}


,但我一直在犯这个错误。现在,
.jar
正在创建这个文件,
ASTParser.class
ASTParser$1.class
,我还不完全理解,但我认为这还不足以破坏它。有经验的人有什么指导吗

问题不在于您的maven设置。代码使用AST解析器的方式存在错误。您将在第25行收到一个
非法状态异常

final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
根据,在以下情况下,在
createAST
上引发此异常:

IllegalStateException-如果提供的设置不足、相互矛盾或不受支持


另一个线索在异常消息中:“未指定源”。在调用
createAST

之前,您需要设置一个源代码,可能是通过使用
parser.setSource()
,但为什么我一开始就有这个!!!非常感谢。现在它有了完美的意义。。。
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);