I'm working on a JRuby (on Rails) project that is depending on various java libraries, imported as jars. Unfortunately, the class name and path of some of the classes I'd like to use are exactly the same in two libraries.
How can I solve this conflict and specify in every case precisely which class from which jar I want? Or can I include a library scoped?
I believe you can put them into packages. For example, in you library1.jar, you would put package com.lib1 or something like that. Then, you could import it with import com.lib1.library1.
Related
I have a jar that works on Java 8.
I would like to create a new jar, that is going to be Multi-Release JAR but empty, just with 'patched' classes in META-INF/versions.
I would like to have a separate jar, so people can include it on Java9, otherwise, they use the default one. Why? Because so many tools are not yet prepared for Java9 MR-Jars.
Would this be possible? Would Java9 MR-Jar override classes from others jars?
Why?
The idea behind Multi-Release jars is that they provide simple patching. In my humble opinion, the way MR jars works is not satisfying.
There are two reasons why I can't make 2 separate Jars:
try to make cross-compile source base that works with Java8 and Java9. You would end up with folders like java, java8 and java9... and then have the build produce two jars, two poms... Yeah, good luck.
Imagine that I even build a library for java9. What about transient dependencies? That would mean that all other libraries that uses mine, would need to have jre8 version that depends on my jre8 version. Just because there is Java9 version!
Here is the story:
My A is a Java library built on Java8 but packaged as Multi-Release Jar which means it contains additional classes for when jar is run on Java9. Additional classes are built separately on JDK9 and I copied them manually (yeah, I know, but it works for now).
Unfortunately, some tools and servers (Jetty) are not aware of MR Jars and this makes them NOT working.
For that reason, I have A-jre8 version of my library, that comes without any extra classes, so servers can use it.
However, if user is using library B that depends on my A, he will still get the MRJar version of A and this will fail again. I want to be able to prevent this somehow. And I can't say to B: hey, could you make B-jre8?
Possible solution
JAR is just about packaging!
Allow the separate jar to patch existing jar.
In my case, I would just include A.jar9 and Java would consider A.jar and A.jar9 together as a package. No need for META-INF/versions. Very clean. And, best of all, it would help in situations like above! If run on Java8, the jar9 jar would make no difference; if run on Java9 the jar9 jar would patch the jar with the same name. Simple as that. No transitive dependency hell.
Rename classes in META-INF/versions.
Common Oracle, have you ever heard about the classpath scanning? Could you at least rename the classes in versions to e.g. *.class9 so not to be caught by existing classpath scanners.
As it is today (Java v9.0.4) - no.
I have to import npm modules in my existing project of Meteor. I am using some modules on each page and in meteor I am forces to import npm module on each page.Is there any common page where I import modules once and use it in my application throughout?
The reason you must import modules (regardless if they are npm packages, atmosphere packages, or other files in your project) in every JavaScript file in Meteor is because you have the ecmascript package installed.
This wonderful package allows you to take advantage of all the great new ECMAScript 2015 (or ES6) features (eg. arrow functions, classes, constants, block scoping​, etc.). One such feature (and the one that you are talking about) that it also includes is modules.
In ES6, modules are a built-in construct where units of reusable code are scoped at the file level such that there is exactly one module per file and one file per module. This means that in order to use any piece of code defined outside of a file you must first import it. This is very similar to import in Java and #include in C++, but subtly different. You can learn more about ES6 modules here.
Long story short, there are tons of advantages to the new spec, however if you wish to revert back to the global nature of pre-ES6, you can simply remove the ecmascript package from your meteor project, follow the original folder structure​ guidelines, and you will no longer have to import modules in every file..
I am working at a library needing some dependencies.
For ease of deployment, I want to create a JAR file containing everything, including the dependencies.
I have tried sbt-assembly - this works, but it may be inadvisable due to legal reasons, so I'm looking for a solution where the resulting JAR file has the original JAR files inside, and where the classpath entry in MANIFEST.MF is set up such that client classes may just add this "nested JAR file" into their classpaths.
Is something like this even possible? sbt-one-jar nearly does, what I want, but only for executables - my product will result in a library, so this is not a perfect fit.
As I've used SBT so far, an SBT plugin would be easiest to use, as it is rather too much work to convert everyting to maven or gradle or ... now.
After thinking a bit more about how class lookup works, we dediced to abandon this experiment.
Basically classes are loaded by ClassLoader instances, and the standard class loaders for applications use a fixed strategy of how to find classes in JAR files or directories.
It seems that to allow a library to be located in a hierarchical JAR file, we must also provide the user of this library (i.e. the library client) with a special classloader so that our client may load all needed classes from the hierarchical JAR.
This is too much work to be worth it - the whole idea of a hierarchical JAR was enteratained only to simplify deployment, and having to juggle own classloaders would nullify this simplification.
In short - possible, but probably not worth the effort.
I have created a play module, how I can import the exported jar to normal java project(not play project)?
When I tried it it gives error:
The type play.libs.F$Promise cannot be resolved. It is indirectly referenced from required .class files
You need to include the play libs as well. Have a look at the deployment documentation. activator dist allows you to create a standalone distribution for your entire play-app.
You can also search the jars in the output for the one which contains the classes you need and just add this one jar to your project.
Or: use sbt/maven to manage your dependencies and add the entire play-framework. (seems to be a bit of an overkill though)
By adding Jar file into the Jruby project.Can We use a java classes of the that Jar file into the Jruby?
Yes, you can. I have a project with the GUI build on java (using NetBeans) and I do the rest in ruby, controllers, etc. I only needed to declare the variables (methods) public to access them (I am not 100% shure if you can avoid this). You can make a section with aliases in Ruby to make them look like ruby syntax. Note, that you also have to require the jar in your Ruby code.