I am getting a java.lang.NoClassDefFoundError: com/zaxxer/hikari/HikariDataSource error when launching my Java plugin on spigot. from what I can tell I have correctly imported it.
I know this question has been posted multiple times but by looking through 3-4 I do not see any clear answer to what is wrong.
The code crashes at hikari = new HikariDataSource() which is also the first hikari statement used.
My Pom
<groupId>drhampust.github.io</groupId>
<artifactId>Blank</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshot/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.14.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
it seems like hikari does not compile into jar but I can use its assets whilst coding.
Code:
import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.sql.*;
public class Main extends JavaPlugin {
//DataBase vars.
public String username, password, database, host, properties, table; //db variables
public int port; //db variable
//Connection vars
private HikariDataSource hikari;
#Override
public void onEnable() {
getLogger().info("onEnable is called!");
loadConfig();
new BukkitRunnable(){
#Override
public void run(){
connectToDatabase();
}
}.runTaskAsynchronously(this);
this.getServer().getPluginManager().registerEvents(new SetterGetter(), this);
}
#Override
public void onDisable() {
getLogger().info("onDisable is called!");
}
public synchronized void connectToDatabase() {
//Database details
String address = getConfig().getString("Database.address");
String name = getConfig().getString("Database.Name");
String username = getConfig().getString("Database.username");
String password = getConfig().getString("Database.password");
int port = getConfig().getInt("Database.port");
//Initialise hikari instace
hikari = new HikariDataSource();
//Setting Hikari properties
hikari.setMaximumPoolSize(10);
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hikari.addDataSourceProperty("serverName", address);
hikari.addDataSourceProperty("port", port);
hikari.addDataSourceProperty("databaseName", name);
hikari.addDataSourceProperty("user", username);
hikari.addDataSourceProperty("password", password);
}
public void loadConfig() {
getConfig().options().copyDefaults(true);
saveConfig();
}
}
full crash report in a paste bin to make it easier to see:
https://pastebin.com/JEMz0f6T
My attempt to create a standalone program using hikaricp:
Code:
package test;
import com.zaxxer.hikari.HikariDataSource;
public class Main
{
public static void main(String[] args) {
HikariDataSource hikari = new HikariDataSource();
//Setting Hikari properties
hikari.setMaximumPoolSize(10);
hikari.setDriverClassName("com.mysql.jdbc.Driver");
hikari.setJdbcUrl("jdbc:mysql://" + "localhost" + ":" + "3306" + "/" + "plugin");
hikari.setUsername("Server1");
hikari.setPassword("227VU07dickCQjav");
}
}
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test</groupId>
<artifactId>Test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>test.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
stacktrace: https://pastebin.com/fNQ7EFnQ
I hav posted an issue on the Hikaricp Github, it might not fit on there as it seems to be a problem only for me, but hey! maybe he can help.
I do not know why this is happening, i suspect that other people can complie HikariCP just fine, the question is why dosen't it work for me...
Edit: it seems like something similar has happend before:
https://www.spigotmc.org/threads/hikaricp-with-spigot-not-importing-with-the-jar.246851/ Checking if I can get my problem to fix itself using this information.
Okay By adding the new arguments to my pom.xml i have now removed the
NoClassDefFoundError and have now gotten this: https://pastebin.com/9DU9Tqra
But hey its a warning not a crash dump. meaning that it worked yay!
Missing details? Ask and I will see what i can do.
#Hampus, I have tried and check that in the version 3.4.0 of Hicari Pool, the class com.zaxxer.hikari.HikariDataSource is available in the package com/zaxxer/hikari. You can see the screenshot below. What I doubt is the repository you have added. Please remove the following from pom.xml and try.
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshot/</url>
</repository>
</repositories>
I know that you may be using your your nexus, this jar file is available in maven central. I provide below the mvn repository link for this.
https://mvnrepository.com/artifact/com.zaxxer/HikariCP/3.4.0
Problem is NoClassDefFoundError.
What fixed it? I fixed it following a post on spigotmc:
https://www.spigotmc.org/threads/hikaricp-with-spigot-not-importing-with-the-jar.246851/
and added:
<build>
<finalName>${project.name}</finalName>
<defaultGoal>clean package</defaultGoal>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</plugin>
</plugins>
</build>
to my pom.xml after adding it I used "clean" then "package" using maven and tried it and it worked. however i do get a warning:
[23:02:45] [Craft Scheduler Thread - 0/WARN]: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
[23:02:45] [Craft Scheduler Thread - 0/WARN]: SLF4J: Defaulting to no-operation (NOP) logger implementation
[23:02:45] [Craft Scheduler Thread - 0/WARN]: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
However this is beside the original question.
I will select this as the answer unless someone finds a better solution.
Related
I was working on a project involving reading from another process when I noticed that it kept throwing java.util.NoSuchElementException: No line found, so I made a Maven test application to check that it was Heroku's fault instead of my program's.
src/main/java/com/test/App.java:
package com.test;
import java.util.Arrays;
import java.util.Scanner;
import java.io.IOException;
public class App{
public static void main(String[] args) throws IOException{
System.out.println(new Scanner(new ProcessBuilder(Arrays.asList("java", "A")).start().getInputStream()).nextLine());
}
}
A.java:
public class A{
public static void main(String[] args){
System.out.println("Test");
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<name>test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.test.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
After javac A.java, mvn install, and java -jar target/test-1.0.jar, it prints "Test" on my computer, but throws a NoSuchElementException in Heroku.
Full Stacktrace:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at com.test.App.main(App.java:7)
Does anyone know what's causing this and how to fix it?
Edit: Attatched Github repo to hopefully make it clearer
Edit Edit: Oddly, if I put A.java in src/main/java and use the command from #Malax's answer, it works, but I still don't get why Heroku isn't seeing the java file outside of src. Maybe it's trimming them out to save space?
The issue occurs because you're calling nextLine on Scanner without checking if there even is a line. You need to check with hasNext to ensure nextLine will not fail.
The underlying reason why you get no lines is that you're not specifying a classpath when invoking java. java will not be able to find your compiled A class without it and fail with something like
Error: Could not find or load main class A
Caused by: java.lang.ClassNotFoundException: A
on STDERR which you don't capture with getInputStream. You will be able to read the error message when you use getErrorStream instead (but then you won't get the STDOUT messages).
This has nothing to do with Heroku and I can only speculate why this is working for you locally. To provide java a classpath, you can use -cp:
ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList("java", "-cp", "target/classes", "A"));
Scanner scanner = new Scanner(processBuilder.start().getInputStream());
while (scanner.hasNext()) {
System.out.println(scanner.nextLine());
}
I can not generate proper WAR file for Tomcat.
I am using MAVEN 3.6.1, Java 12.0.1 and IDE Eclipse. My app is working fine when I run it in eclipse (Run as > Spring Boot App) but the problem is when I am trying to run my WAR file after generate it.
I am doing java -jar .war and I am getting:
Error: Could not find or load main class com.blw.linemanager.Application
Caused by: java.lang.ClassNotFoundException: com.blw.linemanager.Application
I was googling and reading stackoverflow cause I found many post about it but still can not run it. What I am doing wrong?
After some reading I figured out that I have some how configure maven-war-plugin (am I right?) and in pom I did some changes but it does not help.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>bwl</groupId>
<artifactId>LineManager</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<start-class>com.blw.linemanager.Application</start-class>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<build>
<finalName>LineManager</finalName>
<outputDirectory>${project.build.directory}</outputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>12</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources/META-INF</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<classpathPrefix>${project.build.directory}/WEB-INF/classes</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>com.blw.linemanager.Application</mainClass>
</manifest>
<manifestFile>${project.build.directory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Also because on the beginning I was getting error 'no main manifest attribute' I add it and now it looks like this
Manifest-Version: 1.0
Created-By: Apache Maven ${maven.version}
Build-Jdk: ${java.version}
Is my way of think wrong? Should I be able to run .war file as java -jar .war or this is missunderstanding?
#SpringBootApplication
public class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {
public static void main(String [] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
It is better to get a working spring boot application from here to avoid versions conflicts and the like.
For a war-file deployment (into a local tomcat for example), your application must extend SpringBootServletInitializer:
#SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootTomcatApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringBootTomcatApplication.class);
}
}
Change in your pom the packaging to war.
<packaging>war</packaging>
To generate a war-file you need just the spring-boot-maven-plugin:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
And yes you don't need the maven-war-plugin. Just for testing purposes let's annotate our application with #RestController and introduce a simple endpoint:
#RestController
#SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
...
#RequestMapping(value = "/")
public String hello() {
return "Hello World from Tomcat";
}
}
In the Local terminal (in eclipse Ctrl+Alt+T) just enter mvn package than copy the generated war-file from target folder, paste it under the webapps folder of your local Tomcat, request http://localhost:8080/{your-application-name} and you should see the message Hello World from Tomcat.
Now you can add the dependencies you need and continue coding.
Add packaging type with version detail in pom.xml like
<packaging>jar</packaging>
or
<packaging>war</packaging>
Then, append build tag to define name of jar / war like,
<build>
<finalName>YOUR-APP_NAME</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Then, run your application with goal
clean package
to create jar/war in target folder.
Rename your war file to ROOT like ROOT.war
Last step to copy your created jar/war in tomcat webapps folder and start tomcat.
I am new to spring and maven. I have a simple hello world project using Spring . The project builds successfully but i face the following error at runtime:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
This is the main app: App.java
package com.test.SpringMaven2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
System.out.println( "Hello World!" );
ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld hello = (HelloWorld) context.getBean("helloWorld");
hello.setName("Adnan");
hello.getName();
}
}
This is the HelloWorld bean
package com.test.SpringMaven2;
public class HelloWorld{
private String name;
public void setName(String name){
this.name = name;
}
public void getName(){
System.out.println("Hello, " + name);
}
}
This is the annotation based configuration file:
package com.test.SpringMaven2;
import org.springframework.context.annotation.*;
#Configuration
public class HelloWorldConfig{
#Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
This is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>SpringMaven2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringMaven2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
I am running the following command in CMD to run the application:
java -cp {nameofresultjarfile} com.test.SpringMaven2.App
But getting the error messsage above
Any advice?
Thanks
Your pom.xml creates a jar file which contains only the classes defined in your project. But all the other dependencies (spring-core, spring-context-support) which are required by your application aren't included.
If your application is started within eclipse, eclipse integrates these required jar files to the classpath of the application and so it is able to run.
If your application is started from CMD the spring classes can't be found in the classpath and you get a java.lang.NoClassDefFoundError.
It's possible to name all the required jars manually when the application is started from CMD (classpath), but is much easier to created a so called self contained jar file, which has all of them already included. This can be done using maven-assembly-plugin.
An example how to create a self contained jar file can be found here.
The application in a self contained jar can be started now from CMD:
java -jar name_of_your_project-jar-with-dependencies.jar
I was getting the same error when running from Intellij as a main class (In Spring Boot project)
After opening Module settings, In the Deopendencies tab I saw that for spring-context for some reason the scope was provided, changing to compile/ even removing the scope fixed the issue for for me
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>## Put your desired version here ##</version>
</dependency>
In my similar case both #jaysee answer or:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.MyMainClass </mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
or
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.0</version>
helped.
The below fixed similar issue for me.
build plugins section should be put right after /dependencies:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.4</version>
<configuration>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I was facing the same issue. I am using the intellij 2021 but the above solution didn't work. Also in eclipse it was working fine . The solution that worked for me was:
1)Go to edit run/debug configuraiton
2)Modify option
3)Check "include dependency with provided scope"
Today I've spent some non-zero time trying to setup a simplest maven project that will run a simplest jmockit test.
While trying to write such an xml, I've faced with several problems, starting with
java.lang.NoClassDefFoundError: org.junit.runner.Runner
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:61)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
....
and later having problems with running it.
Unfortunately attempts to find quick answer using google didn't help.
So, what it the smallest pom.xml for using jmockit framework with maven?
At the end I came up with a working pom.xml which I want to share - probably this will be useful for someone.
$ cat pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>jmockit-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/1.17/jmockit-1.17.jar</argLine>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
And source files are:
$ cat src/main/java/com/test/jmock/DataProvider.java
package com.test.jmock;
public interface DataProvider {
Integer getInt();
Boolean getBoolean();
}
and
$ cat src/test/java/com/test/jmock/TrivialTest.java
package com.test.jmock;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public final class TrivialTest {
#Mocked
private DataProvider provider;
#Test
public void test() {
init();
Integer mockIntData = provider.getInt();
System.out.println("Mock int data is " + mockIntData);
assertEquals("Unexpected result", mockIntData, Integer.valueOf(12345));
Boolean mockBoolData = provider.getBoolean();
System.out.println("Mock bool data is " + mockBoolData);
assertEquals("Unexpected result", mockBoolData, Boolean.TRUE);
}
private void init() {
new NonStrictExpectations() {
{
provider.getInt();
result = 12345;
provider.getBoolean();
result = Boolean.TRUE;
}
};
}
}
Now this works as expected!
$ mvn test
...
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.025 sec - in com.test.jmock.TrivialTest
I had the same problem. I used the answer from #andrew-krasny and modified it to use the other solution (-Djdk.attach.allowAttachSelf) so you don't have to update it when you update jmockit.
<build>
<plugins>
<!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>10</source> <!-- was 1.8 -->
<target>10</target> <!-- was 1.8 -->
</configuration>
</plugin>
<!-- FIX START -->
<!-- Test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<argLine>-Djdk.attach.allowAttachSelf</argLine>
</configuration>
</plugin>
<!-- FIX END -->
</plugins>
</build>
I have recently set up a pseudo-distributed hadoop 2.2.0 cluster on my Mac OSX following this guide. Then, I tried the basic Cascading file copy with Cascading 2.5.1 However when I compiled the project using maven, I got the following error:
[ERROR] /Users/david/IdeaProjects//CascadingIntro/src/main/java/com/example/CascadingIntro.java:[24,24]
cannot access org.apache.hadoop.mapred.JobConf
class file for org.apache.hadoop.mapred.JobConf not found
What am I doing wrong and how do I fix this? I believe that Cascading 2.5.1 is compatible with Hadoop 2.2.0 from this page on Cascading.org.
My pom.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CascadingIntro</groupId>
<artifactId>CascadingIntro</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>conjars.org</id>
<url>http://conjars.org/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>cascading</groupId>
<artifactId>cascading-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>cascading</groupId>
<artifactId>cascading-hadoop</artifactId>
<version>2.5.1</version>
</dependency>
</dependencies>
<build>
<finalName>CascadingIntro</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.CascadingIntro</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
And in my CascadingIntro class:
package com.example;
import cascading.flow.FlowDef;
import cascading.flow.hadoop.HadoopFlowConnector;
import cascading.pipe.Pipe;
import cascading.property.AppProps;
import cascading.scheme.hadoop.TextDelimited;
import cascading.tap.Tap;
import cascading.tap.hadoop.Hfs;
import java.util.Properties;
public class CascadingIntro {
public static void main(String[] args) {
Properties properties = new Properties();
AppProps.setApplicationJarClass( properties, CascadingIntro.class );
HadoopFlowConnector flowConnector = new HadoopFlowConnector( properties );
String inputPath = args[0];
Tap inputTap = new Hfs(new TextDelimited(true,"\t"), inputPath);
String outputPath = args[1];
Tap outputTap = new Hfs(new TextDelimited(true,"\t"),outputPath);
Pipe copyPipe = new Pipe("copy");
FlowDef flowDef = FlowDef
.flowDef()
.addSource(copyPipe,inputTap)
.addTailSink(copyPipe,outputTap);
flowConnector.connect(flowDef).complete();
}
}
You need to add hadoop-client to your dependencies:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.2.0</version>
</dependency>