Dependency issues, we've all dealt with them, but I'm mostly used to C# and now working in Java so I have some questions.
Let's say I add a library to my project, called ExtLib.
ExtLib has a certain library included in its lib-folder, let's call it LogLib-1.0.
I'm using Eclipse and I've made a User Library for ExtLib, included its main jar file and all of the files in its lib-folder. So far so good.
But now I want to do some logging of my own, so I make another User Library and add the newer LogLib-1.1 to it, because it has some new features I want to use.
Can I ever be sure I'm not breaking ExtLib this way?
I know .NET uses the Global Assembly Cache and methods like that, but I have no clue how Java handles this. I tried Googling, but didn't find much, a few mentions of the Classloader here and there, but nothing helpful.
Can anyone tell me what a proper way to deal with this issue is? Or is it no issue at all?
In this specific case (LogLib-1.0 and LogLib-1.1) we're dealing with the same library that is both a direct dependency of your application, and a "transitive" dependency via the ExtLib. In this situation, dependency management can come to help.
It will probably reason that LogLib-1.1 is a backward compatible release of LogLib-1.0, and it will decide that your application can run fine using only LogLib-1.1.
In the Java world, tools like Maven, Gradle or SBT exist to help you in this. Maven is the most widespread, and other tools often are compatible with Maven.
Usage
To solve this situation using Maven, you would add a file called pom.xml to your application, stating it depends on LogLib version 1.1. That might look like this (note that this example is pure fiction):
<dependency>
<groupId>org.loglib</groupId>
<artifactId>loglib</artifactId>
<version>1.1</version>
</dependency>
The ExtLib you're using also has a pom.xml shipped with it, and it might state
<dependency>
<groupId>org.loglib</groupId>
<artifactId>loglib</artifactId>
<version>1.0</version>
</dependency>
Maven (or any other tool) would decide that including LogLib-1.1 is sufficient to get your application running. When using Maven, mvn depedency:tree helps you visualise that.
Deployment
With respect to the packaging / deployment question: mvn package will package your application to a jar, war or ear archive, including only the dependencies you need (and not two versions of the same lib). This makes you don't have to worry about the order in which your application server reads the jar files.
Related
I am very new to programming, and while working on my first software development project I came across the error: "package javax.activation is not visible." I have read other posts that have said that in order to fix the error, one must add a dependency to the module path?
Because I'm just starting out, I really don't know what this means and how to go about that, and was wondering if someone could point me in the right direction? Thanks in advance. (I'm also using JGrasp if that matters)
Broadly speaking, a dependency is code (often written by someone else) which your application needs to compile.
A dependency in your case, is a java library which has classes that need to be on your class path. You can find more about class paths here: https://docs.oracle.com/javase/tutorial/essential/environment/paths.html. In order to overcome error mentioned in a question, you need to have javax.activation module dependency on your class path.
You could do it in few ways. In the majority of IDEs (in your case we are talking about JGrasp) there is a way to add a dependency to the project directly. Then, your IDE would compile the code with given dependency on a class path and problem would be solved. And that would be the first and most beginner-friendly way, unfortunately I haven't a faintest idea about JGrasp so I'm going to focus on other solutions.
The second way you could do this is to build your program with build automation tool, such as Apache Maven or Gradle. You should definitely check those guys out, as they are insanely useful when it comes to building Java code and, sooner or later, you will probably start using them anyway. Let's say you have chosen Apache Maven. In your project you would then have a pom.xml file and you would simply look-up the needed dependency in Maven Central repository, add it to your dependencies section in pom.xml file and build the application. Your pom would look something like this:
<project>
...
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
</dependencies>
...
</project>
Of course Apache Maven is not a lightweight tool so you would have to take some time to learn how to build code with it. I recommend starting with this tutorial:
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
I also encourage you to get your hands on Apache Maven docs, as it is quite readable and transparent.
And the last way I can think of is to manually compile your application and include the required dependency during the compilation process. There are plenty of tutorials on SO that tell you how to do this, so I'll simply summarize and indicate the resources. What you need to do is to find the required dependency jar package. You will want to search the maven central repository (see: https://mvnrepository.com/) and from there download your .jar file. The next thing you need to do is to learn how to compile your Java code to .class files including the downloaded jar. To acquire such a wonderful skill, please see this one: How to include jar files with java file and compile in command prompt
Amongst those three ways, the recommended one is to get to know with build tools such as Apache Maven or Gradle. Hope I helped you! Good luck
I am very new to Java. I am running somebody else's program on my computer, and they have imports like:
import weka.classifiers.CostMatrix;
import weka.classifiers.Evaluation;
import weka.classifiers.meta.CostSensitiveClassifier;
import weka.core.*;
The program actually works for me, but I am surprised because weka is a pretty specialized program, so I doubt it is distributed with Java. I never installed weka using any package manager, and I have searched the program code and it doesn't contain any weka packages explicitly.
Do you have any tips for figuring out 1) where these packages are installed, and 2) how I "got" these packages on my local computer? I have read that Java doesn't have a centralized package manager like Python or Perl do, so that might make it harder. I am super new to Java so any basic tips about package management would also be appreciated.
These packages are dependencies of your project, so they have probably been downloaded automatically by a tool that manages dependencies.
There are several possible build tools that can do that. Since you are working with Java/JVM, the usual suspects are Maven and Ant or maybe (less likely) Gradle or SBT.
In your case, the most probable scenario is:
A Maven plugin somewhere in your IDE manages the dependencies and downloads the jars (mvn in console less likely: you would have noticed if you used it)
A pom.xml build definition file lists all the dependencies
A weka dependency is probably declared somewhere in the pom, it should look roughly like this:
-
<dependency>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-stable</artifactId>
<version>3.8.0</version>
</dependency>
The JARs are stored in a hidden directory .m2 (or maybe .ivy) in your home directory.
The idea is that you can simply get the source code files and the pom.xml, and let Maven (or a similar build tool) download all dependencies, get all the required compiler plugins (or test-coverage tools, or whatever), and build your project. If you tried to do without a build tool, you would have to pass around eternally long lists of dependencies with version numbers that have to be obtained somehow before your program can be compiled, and this would be just a huge mess.
Edit: It is probably downloaded from here: Maven Central: weka-stable
It wouldn't run unless those packages are on the classpath and passed at runtime via
java -classpath
Or you're running an uber JAR file that does contain the libraries.
Common solutions for dependency management include a pom.xml (Maven), build.gradle (Gradle), or build.sbt (SBT).
While those aren't the only options, another solution would be those JAR libraries have been copied into your Java installation somehow
I've been working on a rather "standard" Java web application for a long time now. I develop in Eclipse using Eclipse's server plugin to run the app in Tomcat. The app's setup is straight forward: Spring for bootstrapping, Wicket for web, Hibernate for ORM, Maven for dependency management.
Today I have added Akka 2.0 to the project. I added it to my POM as per the manual:
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor</artifactId>
<version>2.0.2</version>
</dependency>
Maven finds the dependency and I can see it showing up in the Maven dependencies in Eclipse's package explorer. The referenced Scala Library also shows up (version 2.9.2 as it seems).
I can use the library just as one would expect: Eclipse finds the classes, I can jump to source files etc. Everything works perfectly. But once I start the app and it comes across any part of the program with references to Akka it throws a NoClassDefFoundError.
Since all other libraries still work as expected, my best guess is that is has something to do with the fact that Akka is a library developed in Scala. Since I've hardly used Scala myself though, I could not find any solution to the issue myself and Google isn't really that helpful when it comes to such generic exceptions.
Do you have any advice?
Verify that the required library (AKKA) is in your deployment assembly under Eclipse: open the project's properties and look for "Deployment Assembly" on the left.
[I'm using Eclipse Indigo]
You could verify the presence (or lack) of the expected jar file by examining the deployment under tomcat.
I'm new to Maven, using the m2e plugin for Eclipse. I'm still wrapping my head around Maven, but it seems like whenever I need to import a new library, like java.util.List, now I have to manually go through the hassle of finding the right repository for the jar and adding it to the dependencies in the POM. This seems like a major hassle, especially since some jars can't be found in public repositories, so they have to be uploaded into the local repository.
Am I missing something about Maven in Eclipse? Is there a way to automatically update the POM when Eclipse automatically imports a new library?
I'm trying to understand how using Maven saves time/effort...
You picked a bad example. Portions of the actual Java Library that come with the Java Standard Runtime are there regardless of Maven configuration.
With that in mind, if you wanted to add something external, say Log4j, then you would need to add a project dependency on Log4j. Maven would then take the dependency information and create a "signature" to search for, first in the local cache, and then in the external repositories.
Such a signature might look like
groupId:artifactId:version
or perhaps
groupId:artifactId:version:classifier
This identifies a maven "module" which will then be downloaded and configured into your system. Once in place it adds all of the classes within the module to your configured project.
Maven principally saves time in downloading and organizing JAR files in your build. By defining a "standard" project layout and a "standard" build order, Maven eliminates a lot of the guesswork in the "why isn't my project building" sweepstakes. Also, you can use neat commands like "mvn dependency:tree" to print out a list of all the JARs your project depends on, recursively.
Warning note: If you are using the M2E plugin and Eclipse, you may also run into problems with the plugin itself. The 1.0 version (hosted at eclipse.org) was much less friendly than the previous 0.12 version (hosted at Sonatype). You can get around this to some extent by downloading and installing the "standalone" version of Maven from apache (maven.apache.org) and running Maven from the command line. This is actually much more stable than trying to run Maven inside Eclipse (in my personal experience) and may save you some pain as you try to learn about Maven.
I'm currently working on a Java class whose dependencies are resolved by maven. I now try to integrate the class into a JRuby script.
Is it possible to manage the java dependencies such that the corresponding .jar files and the Java class file are handed over to JRuby automatically?
I have tried to call JRuby from maven, but wasn't able to fix the dependencies. Is there a possibility of handing over the correct maven class path inside the pom.xml file?
Thanks a lot, Philipp
It's not quite what you're after, but you might take a look at this question and answer and compile your Java 'with dependencies', using the assembly:single plugin/goal. Then your JRuby scripts only need to reference a single jar to work.
Whether it's a useful idea or not is dependent on your needs, and how you want to distribute your scripts.
<dependency>
<groupId>org.jruby</groupId>
<artifactId>jruby</artifactId>
<version>1.7.10</version>
</dependency>
Add this to pom.xml and try. This should automatically pull the Jruby jar for your project.