Java Maven:未指定源。。。但是为什么呢?
好的,这是我的第一个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 (
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);