Can't create an instance of the OracleDataSource class - java

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.

Related

Eclipse unable to resolve maven test dependency

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.

Is there a way to replace a maven dependency formerly hosted by pyx4me.com?

i am trying to build a large java codebase that uses maven as it's build tool.
However, building (mvn clean install -DskipTests) always results in the following error:
[ERROR] Failed to execute goal com.pyx4j:maven-junction-plugin:1.0.3:unlink (unlink) on project flink-dist_2.10: Execution unlink of goal com.pyx4j:maven-junction-plugin:1.0.3:unlink failed: Plugin com.pyx4j:maven-junction-plugin:1.0.3 or one of its dependencies could not be resolved: Failed to collect dependencies for com.pyx4j:maven-junction-plugin:jar:1.0.3 (): Failed to read artifact descriptor for sysinternals:junction:exe:1.04: Could not transfer artifact sysinternals:junction:pom:1.04 from/to pyx4me-web (http://www.pyx4me.com/maven2): Connection to http://www.pyx4me.com refused: Connection refused (Connection refused) -> [Help 1]
I don't really know anything about maven, so i'm a litte lost on what to do.
I've tried to manually install the maven-junction-plugin in the local .m2 directory. However, that was before i understood that it's not the .jar that is missing, but some dependency of that jar.
I would be really grateful for any hints or insight from someone that knows anything about maven.
The problem you are having is the following: even though, the maven-junction-plugin artifact, version 1.0.3 is available in maven central repository, it tries to download a dependency called sysinternals:junction:exe:1.04 from (http://www.pyx4me.com/maven2) but the connection times out (You can also try to ping pyx4me.com and you will find out it is not responsive).
The maven-junction-plugin was in use by the Apache Flink before 1.2 Release.
The current stable versions of Apache Flink do not use it anymore. In fact the project seems abandonned and not supported anymore.
Please see the following maven's central Jira ticket asking for a removal of it from maven central.
So it looks like there is no simple way to do what you are trying to do.
But you might be able to hack a bit around it.
First it looks like systinternal:junction:1.04 is not available anywhere, but the 1.07 version is available here (it is possible that it won't be compatible with maven-junction-plugin 1.04, so you need to test it enough to make sure it is)
The zip contains the .exe in two flavors (a 32 and a 64 bits versions). Use the version that suits your need and install it in your local repository.
The general approach to install an artifact in your local repository would be the one described here, but since we are dealing with an .exe file you would rather use the procedure described here
Now let's assume that you have successfully installed the systinernal:junction:1.07 in your local repository with the following artifact properties:
groupId: your-group-id
artifactId: junction
version: 1.07
type: exe
You can use the jar installation in local repository approach to install the plugin's jar along with a customized version of the pom where you point to your local version of the systinternal:junction dependency instead of the original version. You can find both files here.
The dependencies section of your customized pom will then look like:
<dependencies>
<dependency>
<groupId>your-group-id</groupId>
<artifactId>junction</artifactId>
<version>1.07</version>
<type>exe</type>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies>
you can remove the old repositories section as the remote repository is not working anyway.
In my opinion, it is too much of a hacking (and you will probably have hard time convincing your colleagues to bring something like this in production). So I suggest to try to find an equivalent of the functionalities your are using this plugin for.

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 come my pom.xml dependency isn't found for my Jenkins plugin?

My pom.xml has this dependency:
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
When I use the XMLSerializer it throws an exception: java.lang.NoClassDefFoundError: nu/xom/Node
If I run the class locally and add the JAR to my classpath, everything works as expected. I'm running this class as a Jenkins plugin so I don't expect to be manually defining classpath - I figured that's what Maven should be handling.
It's important to note that Jenkins plugins require me uploading an hpi file that is created from Maven. It is not running based on the output jar. If I go on Jenkins box and manually put the xom JAR into WEB-INF/libs, it works. But obviously that means this plugin wouldn't for other people, which is self-defeating.
Here is minimal code causing error: https://github.com/DaveStein/parser-sample
The Readme has exact repro steps.
Note on chosen answer
The PR to my sample repo got me most of the way to where I needed to be. I did have a few other issues that had to get resolved, but the JSONObject conflict was the core problem. I took out all GlobalConfiguration as Jesse's PR suggested. The only other issue that might concern a future viewer was some glitch when using xom as explicit dependency while also using a higher version than 1.626 for org.jenkins-ci.plugins at the time of this post.
Jenkins core bundles json-lib. (A forked copy, not that it matters for purposes of this question.) It does not bundle the optional dependency¹ XOM, whatever that is. When your plugin loads XmlSerializer.class, it gets defined by the class loader for Jenkins core, which then attempts to link against classes such as nu.xom.Node. Since this is not available in the defining loader of XmlSerializer—the Jenkins core class loader (more or less jenkins.war!/WEB-INF/lib/*.jar)—you get an error. The fact that a class by that name is accessible in your plugin class loader is of no import, excuse the pun.
If your plugin needs to use its own versions of classes which are normally bundled in Jenkins core and exposed to plugins implicitly, then it needs to not only bundle those JARs (a regular compile-scoped Maven dependency suffices for that purpose), but to also use the pluginFirstClassLoader option. Before attempting to do so, you had better understand Java class loading semantics thoroughly, or you will be lost in a maze of cryptic² ClassCastExceptions and LinkageErrors.
By the way the mvn hpi:run command normally used to test plugin code iteratively does not simulate a realistic class loading regime. So if you are using pluginFirstClassLoader or any other tricks in this space, always double-check the resulting class loading behavior by (re-)installing an *.hpi in a sample Jenkins instance, for example using /pluginManager/advanced, or the install-plugin CLI command. Judging by your description, you were already doing that (and perhaps unaware of hpi:run).
¹The original sin here is use of optional dependencies. json-lib should rather have defined a distinct artifact json-lib-xom with hard dependencies on json-lib and xom. That would ensure that any given class loader can either see XmlSerializer and its dependencies, or neither.
²No progress on JDK-6273389, alas. Marked as a duplicate, but what it is a duplicate of, I am not sure. Theoretically Java 9 modules make questions like this obsolete—by imposing such onerous restrictions that applications like Jenkins could not use that module system to begin with.
please google "noclassdeffounderror vs class not found" , this error means the class dependency is in fact found but is not available in run time.
Try these steps:
Run mvn clean package and mvn clean install
Check if your maven environment is correct and has latest jars
Check if the installed target project contains the required jars
Check if dependency type is selected as runtime and not only as
compile time in pom.xml
Here is an example of using runtime dependency:
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
<type>bar</type>
<scope>runtime</scope>
</dependency>
My guess is that the local version of the XOM jar is not the same as the one that is used in your Maven version. To verify use the dependency:list Maven command to list out all your dependencies. Verify if the XOM dependency listed is the same version as the local jar.
Probably error on jenkins occurred while this dependency has been loaded for the first time and now it's considered as complete. Try to remove dependency from jenkins' maven local repository and re-run. That might help you

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

Categories