gpt4 book ai didi

java.lang.ClassFormat错误: Invalid superclass index 0 in class file com/github/javaparser/JavaParser

转载 作者:行者123 更新时间:2023-12-05 06:36:20 25 4
gpt4 key购买 nike

我是 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>

mvn clean install on cmd:

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] ------------------------------------------------------------------------

最佳答案

类文件中的父类(super class)索引 0 无效。在我的例子中,通过删除 NetBeans 缓存文件夹解决了这个问题。

C:\....\AppData\Local\NetBeans\Cache

关于java.lang.ClassFormat错误: Invalid superclass index 0 in class file com/github/javaparser/JavaParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49173935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com