我是java的新手。我正在尝试创建一个项目,使用JavaParser从所需的.java文件获取类名,字段名。 但我得到一个错误,我对此错误一无所知。如何修复该错误?
1-)首先,我在NetBeans IDE上创建一个新项目
2-)在项目文件夹中添加JavaParser依赖项pom.xml文件和“mvn clean install”。(可能我做错了)
3-)创建文件并输入以下代码
4-)编译,我得到以下错误:
Exception in thread "main" java.lang.ClassFormatError: Invalid superclass index 0 in class file com/github/javaparser/JavaParser
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at parser.GetTypes.parseClassname(GetTypes.java:28)
at parser.Parser.main(Parser.java:28)
C:\Users\q\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
Parser.java文件(src / parser / Parser.java):
package parser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Parser {
public static void main(String[] args) throws FileNotFoundException {
System.out.println(GetTypes.parseClassname(new File("C:\\HelloOOPP.java")));
}
}
GetTypes.java文件(src / parser / GetTypes.java):
package parser;
import com.github.javaparser.*;
import com.github.javaparser.ast.*;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.lang.reflect.Field;
public class GetTypes {
public static String parseClassname(File filename) throws FileNotFoundException {
FileInputStream fin = new FileInputStream(filename);
CompilationUnit cu = JavaParser.parse(fin);
// String packagePrefix = cu.getPackage().getName().toString();
//if (!packagePrefix.isEmpty()) packagePrefix += ".";
StringBuilder build=new StringBuilder();
for (TypeDeclaration type : cu.getTypes())
{
if (type.isClass())
{
build.append("Class Name:");
build.append(type.getName());
build.append("\n");
build.append("Fields");
//build.append(Get_Fields(type));
}
}
return build.toString();
}
private String Get_Fields(TypeDeclaration c) //
{
Field[] fields = c.getClass().getFields();
StringBuilder str=new StringBuilder();
for(int i=0;i<fields.length;i++)
{
str.append(fields[i].getName());
str.append("\n");
}
return str.toString();
}
/*
private int Count_Fields(TypeDeclaration c)
{
}*/
}
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- This is often your domain name (reversed.) -->
<groupId>com.parser</groupId>
<!-- The name of this project (actually, the name of the artifact, which is the thing that this project produces. A jar in this case.) -->
<artifactId>parser</artifactId>
<!-- The version of this project. SNAPSHOT means "we're still working on it" -->
<version>1.0-SNAPSHOT</version>
<properties>
<!-- Tell Maven we want to use Java 8 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Tell Maven to treat all source files as UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Here are all your dependencies. Currently only one. These are automatically downloaded from https://mvnrepository.com/ -->
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-core</artifactId>
<version>3.5.16</version>
</dependency>
</dependencies>
</project>
cmd上的mvn clean install:
C:\parser>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building parser 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ parser ---
[INFO] Deleting C:\parser\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ parser ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\parser\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ parser ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ parser ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\parser\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ parser ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ parser ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ parser ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: C:\parser\target\parser-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ parser ---
[INFO] Installing C:\parser\target\parser-1.0-SNAPSHOT.jar to C:\Users\q\.m2\repository\com\parser\parser\1.0-SNAPSHOT\parser-1.0-SNAPSHOT.jar
[INFO] Installing C:\parser\pom.xml to C:\Users\q\.m2\repository\com\parser\parser\1.0-SNAPSHOT\parser-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.193 s
[INFO] Finished at: 2018-03-08T16:41:28+03:00
[INFO] Final Memory: 12M/155M
[INFO] ------------------------------------------------------------------------
答案 0 :(得分:0)
我的错。问题是“在我的项目中使用netbeans结构并尝试使用maven添加包”。我创建一个新文件并按照本教程创建文件结构:maven file structure 为这个结构实现我的代码,并且每个人都可以正常工作