Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get classes from one-jar #20

Closed
yadavan88 opened this issue Jul 28, 2015 · 1 comment
Closed

Unable to get classes from one-jar #20

yadavan88 opened this issue Jul 28, 2015 · 1 comment

Comments

@yadavan88
Copy link

I have a scala application and I am packaging it using sbt-one-jar. I am trying to use the ClassUtil to get some annotation details from my jar file. However, I am not able to get the details from one-jar. It is working fine if I run directly in the project. But once the jar is created, it is not getting the classes. I know that the problem is with the one-jar as it packages the jar files in different folder. Is there any way to make the ClassUtil work with one-jar. My sample code with ClassUtil is given below.

def getAnnotations ={
    val finder = ClassFinder()
    println("full list "+finder.getClasses().size)
    finder.getClasses().toList.map(x=> println(" -- "+x))
    val classes = finder.getClasses().filter(c => c.implements("work.com.pack.FunctionMetadata"))
    println("classes size = "+classes.size)
    val allClasses = classes.map { clazz => Class.forName(clazz.name) }
    println("size ="+allClasses.size)
    val fmAnnotations = allClasses.map { clazz =>
      val fields = clazz.getDeclaredFields.filter(field => field.getDeclaredAnnotations.nonEmpty)
      val result = fields.toList.map { field =>
        val result = field.getDeclaredAnnotations.find(a => a.annotationType().getName == classOf[MyAnnotation].getName)
        if (result.isDefined) Some(result.get.asInstanceOf[MyAnnotation]) else None
      }
      result.filter(_.isDefined).map(_.get)
    }.toList.flatten
    println("Annotations = "+fmAnnotations)
    fmAnnotations
  }

The output I am getting is from OneJar classes. Output from running the above code is

full list 20
com.simontuffs.onejar.Handler$1
-- com.simontuffs.onejar.OneJarFile
-- com.simontuffs.onejar.OneJarFile$2
-- com.simontuffs.onejar.JarClassLoader$2
-- com.simontuffs.onejar.OneJarFile$1
-- com.simontuffs.onejar.Boot
-- com.simontuffs.onejar.Boot$3
-- com.simontuffs.onejar.JarClassLoader$1
-- com.simontuffs.onejar.JarClassLoader$OneJarURLFactory
-- com.simontuffs.onejar.JarClassLoader$IURLFactory
-- com.simontuffs.onejar.JarClassLoader
-- com.simontuffs.onejar.OneJarURLConnection
-- com.simontuffs.onejar.Handler
-- com.simontuffs.onejar.Boot$1
-- OneJar
-- com.simontuffs.onejar.IProperties
-- com.simontuffs.onejar.JarClassLoader$ByteCode
-- com.simontuffs.onejar.JarClassLoader$FileURLFactory$1
-- com.simontuffs.onejar.JarClassLoader$FileURLFactory
-- com.simontuffs.onejar.Boot$2
classes size = 0
size =0
Annotations = List()
List()

@bmc
Copy link
Owner

bmc commented Apr 7, 2016

This is problematic. One-Jar creates, in effect, a jar of jars, with custom One-Jar-provided classes and a classloader that knows how to dig your real classes out of the One-Jar consolidation.

There's certainly a "way" (several ways, in fact) to write code to make ClassUtil work with this kind of packaging mechanism, but I'm not convinced it's worth the effort. There are other "fat jar" mechanisms out there that don't use this kind of odd approach.

For instance, sbt-assembly also generates fat jars that can be run with java -jar thejar.jar, but they're flattened jars that contain all your dependencies—regular jars, in other words, that contain all the classes in all your dependencies. An sbt-assembly-created jar doesn't add special classes or use a special class loader; it's just a regular jar. There's a Maven Assembly Plugin that does the same thing with a Maven build.

I really don't like the One-Jar approach. Maven Assembly and sbt-assembly are much simpler (with far fewer moving parts in the final packaged jar).

At the moment, I'm disinclined to spend the time supporting One-Jar. If there's enough demand, I may reconsider.

@bmc bmc closed this as completed Apr 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants