Eclipse unable to resolve maven test dependency - java

I'm running into a bit of a strange issue with eclipse 4.18 that I'm hoping someone can help me with. My project is a jetty webapp that does some communication with an external server over FTP. I'm using mockftpserver (https://mockftpserver.org/) to assist with unit testing the code that needs to communicate with the FTP server.
I have the library dependency defined in the pom as
<dependency>
<groupId>org.mockftpserver</groupId>
<artifactId>MockFtpServer</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
I then have an import defined in my test class like
package my.project.test;
import org.mockftpserver.fake.FakeFtpServer;
public class TestGuiceModule extends GuiceModule {
}
Nothing too exotic going on, and maven seems to be able to run the compile. Eclipse however does not seem to be able to see the library. I have a bunch of errors along the lines of
The import org.mockftpserver cannot be resolved.
I double checked the build path and I can see the library listed under 'Maven Dependencies' and the package structure of the jar matches my import statement. Even stranger, I have other 'test' scoped dependencies in the pom.xml and eclipse is detecting those just fine. I'm at a bit of a loss as to why it is having an issue with this one particular library. Any suggestions on what I could do to track this down?

I was eventually able to solve this by deleting my settings folder and .project file and reimporting the project (see: Eclipse throws NullPointerException during Maven update).
The project was originally created in an older version of eclipse, but I thought I would be ok since I was checking out a fresh copy from the git repo. Apparently something must have gotten checked in that the newer version of eclipse didn't like.

Related

Can't create an instance of the OracleDataSource class

I'm working on a Java app, trying to create an instance of the OracleDataSource. I've seen a bunch of posts saying the Oracle drivers aren't in Maven Central but according to this, they are now. So here's the dependency in my pom.xml:
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.3.0.0</version>
</dependency>
Then in my class I have:
import oracle.jdbc.driver.*;
import oracle.jdbc.pool.OracleDataSource;
...
public void someMethod() {
...
OracleDataSource ods = new OracleDataSource();
...
}
But I get errors saying neither the packages nor the class exist.
Error: package oracle.jdbc.pool does not exist.
Type 'OracleDataSource' not found
What am I missing here? It's been a while since I last worked with Java and Maven so I figure it's something simple.
EDIT
The IDE (JDeveloper) and Maven seem to be out of sync. I'm getting different errors running Maven commands.
I tried the dependency and OracleDataSource instantiation as above and it compiled and ran in IntelliJ and on command line (mvn clean package) on Java 11.0.12-open, so I believe you are correct that the Oracle drivers are in Maven Central.
It may be an IDE issue - I sometimes find that IntelliJ's internal view of the project and dependencies is slightly out of kilter and either needs cache invalidation or careful gradual rebuild of any modules. I'm not sure what the equivalents are in JDeveloper.
However, generally the best way to deal with strange compilation issues is to first take the IDE out of the equation - try a mvn clean install in the terminal. If that fails it may be worth moving your ~/.m2/repository directory (or just the ~/.m2/repository/com.oracle directory) and trying again in case a previous failure to retrieve artifacts has been cached. If it still fails then maybe check ~/.m2/settings.xml to make sure it is pointing to maven central, but I'd expect it to find that driver okay.

figure out how I got a Java package

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

How to import git java project to Eclipse

I've tried every option explained step by step here and here: and here
And I can't get it to work.
What I want to do is pick this project: , however it may be done (I've tried both through maven and git), and use its code in eclipse. And what I mean by that, is that I get to the point of seeing the folders in eclipse, but I can't create packages since it's not a java project, and if I mess up the code on the files that appear, it doesn't give me a warning nor in general interacts with said code.
So I guess I'm missing some piece of knowledge and I don't know where else to look for it. What should I do to use that project in my eclipse, and create my own code that calls and uses the classes and methods from said project?
Thank you in advance.
Just download it with git, then import it as a maven project. If this fails, create new "java project from existing sources", pointing as a source the simmetrics-core/src and simmetrics-example/src directories. If it fails, point separately simmetrics-core/src/main, simmetrics-core/src/test etc.
Since this artifact is present in the maven repository (https://mvnrepository.com/artifact/com.github.mpkorstanje/simmetrics/4.1.1), you can create your own new maven project in Eclipse and add simmetrics as a dependency to it in its pom.xml file
<dependency>
<groupId>com.github.mpkorstanje</groupId>
<artifactId>simmetrics</artifactId>
<version>4.1.1</version>
</dependency>
This will put simmetrics to the classpath of your own project and you should be able use its API

NoClassDefFoundError on AppEngine with org.eclipse.core.runtime.Platform

I'm working with EMF on a non-plugin, AppEngine Maven project that has the org.eclipse.core.runtime AND org.eclipse.ocl.ecore dependencies. The project runs perfectly when on the local server (I am using JDK 7). However, when deployed to GAE, I get the following two errors, sometimes one, sometimes the other:
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.core.runtime.Platform
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.ocl.ecore.internal.OCLStandardLibraryImpl
Both classes are indeed present in the JARs in the target folder's lib directory (also unzipped and checked the JARs). Here's how the dependencies look in the POM:
<dependency>
<groupId>org.eclipse.core</groupId>
<artifactId>org.eclipse.core.runtime</artifactId>
<version>${version.core.runtime}</version>
</dependency>
<dependency>
<groupId>org.eclipse.ocl</groupId>
<artifactId>org.eclipse.ocl.ecore</artifactId>
<version>${version.oclecore}</version>
</dependency>
Where could the problem lie? Am I missing something obvious?
Any help would be greatly appreciated!
Both classes are indeed present in the JARs in the target folder's lib
directory (also unzipped and checked the JARs
I can not see your application structure, but wherever these JARs are, if your application needs them to be the same in GAE as they are on your local machine; then you should make sure you are using the same version as present in the target version of GAE........ I used to have that problem because I started using GAE when it was new and client libraries used to change very fast.

How to import org.apache Java dependencies w/ or w/o Maven

So the quick background is I am creating a java program, that uses many different imports
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
I know that Netbeans isn't finding these files because I do not have them on my computer. But is there a way to have Netbeans automatically connect with org.apache and retrieve these files? Or do I just have to go and download them. Someone recommended using Maven, but I am not sure if this is the right solution or how to go about that?
Thanks
Unless you use a Maven structure (see here getting started with Maven) you will have to download all jars manually.
If using only Hadoop (as in your example) this might not seem that much of a deal, but when working with big projects it is easier to declare your dependencies in a pom.xml file. It is much more easier than downloading X different jars, and you can easily move to a newer version of a library, rather than having to delete and and download another.
I saw that someone asked in a comment why people like Maven so much. Well, to be honest, I personally find it easy to use and very useful. Furthermore, a Maven project can be easily imported in IntelliJ, Eclipse or Netbeans, whereas creating for example an IntelliJ project can cause difficulties in importing it in Eclipse or NetBeans.
To get started using Maven with Netbeans, you can go to: New Project, Categories:Maven Projects:{Best Option}. Then in the project files, open pom.xml. Here is where dependencies for your project are added. If you are not sure what to insert try searching for your jar name + "maven" on the internet. The plugin for Netbeans is able to connect to the maven repository and autocomplete most fields.
Sample from: http://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.6
<project...>
....
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
...
</project>
Download the .jar file here: http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/0.20.2
In Eclipse, right-click on your project, click Properties, search in the text box for Library, click on Build Paths, click Add External JAR, and select the file you downloaded from the link above.
You will have to download the jar-files yourself. Unless you start using Maven or a similar dependency management tool.
You must download them. The name org.apache.hadoop is a package name, and we only use the name of the site as a convention. See this tutorial on packages for more information. Essentially a package is a folder on your computer, often in the Java\jre\lib\ext\ directory.
Refer tutorial
http://www.tutorialspoint.com/hadoop/hadoop_mapreduce.htm
It mentions :-
Download Hadoop-core-1.2.1.jar, which is used to compile and execute the MapReduce program. Visit the following link http://mvnrepository.com/artifact/org.apache.hadoop/hadoop-core/1.2.1 to download the jar.
Not a netbeans user , but I'm sure that even in netbeans, you have a maven plugin.
"Mavenize" your project, and when you will perform mvn clean install, you will get these jars to local maven repository.
With Eclipse I use the m2Eclipse plugin and it works really well for me.
This of course depends that these jars can be found in maven repositories over the net, such as maven central repository.
I have final figured out my preferred way to create a new Hadoop project and import the dependencies using Maven.
Using NetBeans I create a new Maven project.
Then under project files, I open the pom.xml.
I finally add inside of
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>0.20.2</version>
</dependency>
After building with dependencies I am now ready to code.

Categories