Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

gradle - ClassNotFoundException on Existing Groovy Classes

I have library of Classes created by a gradle build. They are part of a REST library that was built to interface with github. All of the following classes belong to the same package:

  • FileContentUpload.class
  • GitHubMember.class
  • Labels.class
  • PRstate.class
  • PullRequest.class
  • RateLimitCore.class
  • RateLimitResources.class
  • Repository.class
  • ReviewState.class
  • Tree.class
  • TreeList.class
  • User.class
  • WeeklyContribution.class
  • Blob.class
  • Branch.class
  • BranchUpload.class
  • Commit.class
  • Contents.class
  • Event.class

These classes build and can be used successfully. I need to get the classes in a groovy script to get data out of them. I have tried two different methods to do so. The first method is to go into the folder and get the classes out of the files directly:

File[] files = directory.listFiles()
    for (File file : files) {
        def fileName = file.getName()
        def fullyQualifiedName = file.getAbsolutePath()

        if (file.isDirectory() && !fileName.contains(".")) {
            classes.addAll(findClasses(file));
        } else if (fileName.endsWith(".class") && !fileName.contains("$")) {

            if (fullyQualifiedName.contains("main")) {

                fullyQualifiedName = fullyQualifiedName.substring(fullyQualifiedName.indexOf("main") + 5).replace("\",".")
                fullyQualifiedName = fullyQualifiedName.substring(0,fullyQualifiedName.size() - 6)

                classes.add(Class.forName(fullyQualifiedName));
            }

        }
    }

The second method has been to look within the jar and extract the classses out of that:

        JarFile jar = new JarFile("C:\work\RestLibrary\build\libs\RestLibrary.jar")
        for (j in jar.entries())
        {
            if (j.getName().endsWith(".class") && !j.getName().contains("$"))
            {
                def x = j.getName()
                x = x.replace("/",".").substring(0,x.size() - 6)
                def c = Class.forName(x)
                theList.add(c)
            }
        }

Both of these have the same result. On the classes RateLimitCore and RateLimitResources I get a ClassNotFoundException. Here's what I observe so far:

  • The Jar file contains all of the classes, including these two.
  • All of these classes are in the same package.

I'm really confused as to how those two classes in particular would not exist when they are, as far as I can tell, not any different than any of the rest of the classes in the package. Any suggestion on how to tell what's different about them would be greatly helpful to me.

question from:https://stackoverflow.com/questions/65943683/classnotfoundexception-on-existing-groovy-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...