Writing code on Eclipse with Weka. Trying to use the weka.filters.Filter method but I constantly receive this error:
java.lang.NoClassDefFoundError: weka/filters/Filter
Everything else seems to be imported right and I see the filter class in the Package Exploration.
Thank you to everyone in advance
Haven't used Eclipse in a long time, as I prefer IntelliJ IDEA.
Here is what I did:
Create a new workspace
Create a new Java project
Unchecked Create module-info.java and clicked on Next
Added my external weka.jar under Classpath and clicked on Finish:
Created class Testing.java in package myweka with this content:
package myweka;
import weka.filters.Filter;
import weka.filters.MultiFilter;
public class Testing {
public static void main(String[] args) throws Exception {
Filter f = new MultiFilter();
System.out.println(f);
}
}
Here it is what it looks like in Eclipse:
Executed the class without problems (just outputs the classname in the console).
Instead of simply adding an external jar to your project, you could set up a Maven project. Here are the steps:
Create a new directory to house all your code
Create a file called pom.xml (Maven Project Object Model) and add this content (the libraries that are required for the project are listed inside the dependencies tag, this example uses Weka 3.9.5; the 3.8.5 dependency is commented out; you can only use either or):
<?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>MyGroup</groupId>
<artifactId>MyWeka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyWeka</name>
<description>Project using Weka.</description>
<dependencies>
<!-- Weka 3.9.5 -->
<dependency>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-dev</artifactId>
<version>3.9.5</version>
</dependency>
<!-- Weka 3.8.5 -->
<!--dependency>
<groupId>nz.ac.waikato.cms.weka</groupId>
<artifactId>weka-stable</artifactId>
<version>3.8.5</version>
</dependency-->
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
</plugin>
</plugins>
</build>
</project>
Create directory src/main/java/myweka in the same directory as the pom.xml file.
Place the following content as Testing.java in that directory:
package myweka;
import weka.filters.Filter;
import weka.filters.MultiFilter;
public class Testing {
public static void main(String[] args) throws Exception {
Filter f = new MultiFilter();
System.out.println(f);
}
}
Launch Eclipse and create a new workspace.
Import your project
Import the project as an Existing Maven Project
Select the directory where your pom.xml is located and click on Finish.
Once Eclipse has finished the import, you can execute the myweka.Testing class, which will just output the filter's classname in the console.
Related
I'm new to using maven and have a Project where I'm trying to create an instance of an object but keep getting the error:
error: cannot find symbol
CommentProcessor p = new CommentProcessor();
^
symbol: class CommentProcessor
location: class App
I have the files:
App.java
package com.group.pack;
public class App {
public static void main(String[] args) {
CommentProcessor p = new CommentProcessor();
p.connect();
}
}
CommentProcessor.java
package com.group.pack;
public class CommentProcessor {
public CommentProcessor(){
}
public void connect(){
...
}
}
App.java and commentProcessor.java are both in src/main/java/com.group/pack
If i take the files out into a separate project without the package (and not using maven), it will compile without any issues. Would this be something to do with how maven works?
Ive also tried creating a jar file, but this won't work either.
This is the pom that VSCode generated:
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>com.group.pack</groupId>
<artifactId>pack</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>pack</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Build Settings -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
I've recreated a mock project, I see no issues for me. Try recreating a simple mock project.
note:
I've noticed a problem in your pom.xml file.
When you create a jar file, you will need to create a jar with the dependencies
I am trying to run my tests but I am facing an issue where I cannot run my tests when I run the following command:
mvn clean test
my project contain 3 modules (see image attached):
Every module in the project contains pom.xml file which contains only the dependencies relevant for the module.
the main pom.xml (the reactor) is the file which run the test and control the project, and this is its content:
<?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.hackeruso</groupId>
<artifactId>neo</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>automation-ui</module>
<module>automation-api</module>
<module>morpheus</module>
</modules>
<name>neo</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.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<aspectj.version>1.8.10</aspectj.version>
<testng.version>7.3.0</testng.version>
</properties>
<dependencies>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.13.6</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<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-jar-plugin</artifactId>
<version>3.0.2</version>
</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>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
</argLine>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
when I run the command mvn test I am getting the following message:
No tests to run.
And this image shows where I hold my tests:
src/test/java/com/hackeruso/automation/ui/LoginTest
This is an example for my test class:
--------------------EDIT-------------------------------------------
package com.hackeruso.automation.ui;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class LoginTest extends BaseTest{
#Test(dataProvider = "userDetailsProvider")
public void loginTest(String username, String password){
signIn(username, password);
}
#DataProvider(name = "userDetailsProvider")
public Object[][] userDetailsProvider(){
return new Object[][] {
{"user#mail.com", "*******"}
};
}
}
From documentation:
For example, a project that is purely metadata (packaging value is
pom) only binds goals to the install and deploy phases (for a complete
list of goal-to-build-phase bindings of some of the packaging types,
refer to the Lifecycle Reference).
As you can see, only install and deploy phases (not test) are valid for a pom packaged project.
The Java code should be not there, since a pom project should be purely metadata.
The parent project has a packaging of <packaging>pom</packaging>. This means this is a meta-project and should not contain any source code.
What you need to do is to move the tests in any of the existing modules or create a new one for these tests. By looking at the package structure of the tests, I guess it would be automation-ui in your case.
Then use the following command to run the tests from all the modules
mvn test -am
Where -am will make all the submodules.
If you want to run tests for a single module, use
mvn test -pl <submodule-name>
So eventually what I did was creating another module for testing and the main project module will contain only Metadata as #Yasin suggested, I also deleted the 'src' package from my main project module leaving it just with a pom.xml file which will manage the other modules (the reactor).
Now everything is working as expected.
Thanks!
I'm trying to create a Kafka Streams application with Java and having a hard time managing the dependencies. Please let me say that I am completely noob to Java and it's ecosystem of tools.
The project is compiled with Maven and I'm using IntelliJ Idea. The project is configured to use OpenJDK 14.
Here's the 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.petite</groupId>
<artifactId>ora</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>compile</defaultGoal>
</build>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
</project>
With this setup I can print a "Hello world!" to the console but as soon as I import Kafka Streams classes errors start to pop up:
package com.petite;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.kstream.StreamsBuilder;
import org.apache.kafka.streams.processor.Topology;
public class Main {
public static void main(String[] args) {
System.out.println("Hello from Java");
}
}
IntelliJ tells me that it can't find the symbols StreamsBuilder and Topology. Now being a complete alien to Java, IntelliJ and Maven I really can't find a way to go forward.
How come the dependencies shown on Kafka site (https://kafka.apache.org/25/documentation/streams/developer-guide/write-streams.html#libraries-and-maven-artifacts) can be downloaded but can't be imported?
You StreamsBuilder and Topology are under stream package and not in kstream and processor
Use the below imports :
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.Topology;
And as you mentioned that you are new to Java, just an torubleshoot advice , if you ever face such issues just delete the import then modern IDE like Inteliij will automatically show the option.
I need to use maven (for a school project) to create an executable file from a single maven command. I've never used maven and tried many solutions here on stackoverlow. The solutions created a jar file, but the file never opened.
This is my project structure
src
com
project
code
swing
programm
interface
Main.class
I know this isn't maven convention, however changing it now would mean I would have to adjust the imports (as intelliJ doesn't refactor everything perfectly) for around 40 classes.
<?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>MyGroup</groupId>
<artifactId>myProgramm</artifactId>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Hello World</name>
<description>Course Project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.25.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit/junit5-engine -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit5-engine</artifactId>
<version>5.0.0-ALPHA</version>
</dependency>
</dependencies>
<build>
</build>
What do I have to put inside to make an executable file?
TimurJD's answer is correct however I would like to explain step by step what is actually happening and why.
To have a jar be executable the JVM needs to know where your main method is.
For that you need a file called META-INF/MANIFEST.MF inside the jar you create.
This file must contain a reference to the class containing your main method which is done like this:
Main-Class: com.example.ClassContainingMainMethod
There are many ways of creating said file but since you are using maven here is the plugin that will help you create this manifest file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.my.packege.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Once you have the plugin in your pom.xml simply run the maven install goal, either from your IDE or the command line. After you should find a folder called target in your project. That folder will contain the executable jar.
To run the jar you can call from the command line:
java -jar MyProject.jar
It should also be noted that unless you abide by the maven standard of keeping your source code in src/main/java you will have to specify your source folder explicitly.
You need to add plugin to your pom.xml file
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
and to run the program: mvn clean install exec:java
... here is the link for doc http://www.mojohaus.org/exec-maven-plugin/usage.html
There are possible different solutions, depends on your requirements: https://www.baeldung.com/executable-jar-with-maven
I have a very simple application in Maven that uses Apache Felix as an OSGi Framework.
Here is a pom.xml file content:
<?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.example.installer</groupId>
<artifactId>bundle-installer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>5.6.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
In the main class I am just getting the bundle by its path and
installing it by Apache Felix:
package com.example.installer;
import org.apache.felix.framework.Felix;
import org.osgi.framework.BundleException;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws BundleException {
Properties config = new Properties();
config.setProperty("org.osgi.framework.storage", "/Users/johndoe/plugins");
Felix framework = new Felix(config);
framework.start();
framework.getBundleContext().installBundle("file:/Users/johndoe/Desktop/bundle-example-0.0.0.1.jar");
framework.stop();
}
}
Let me explain what I want to implement. As you see I have changed Felix cache dir to "/Users/johndoe/plugins", i.e. I am going to install plugins programmatically by Felix. This code is working well enough except one thing. After installation each bundle is being installed in the folder like bundle0, bundle1, ..., bundleN etc. But I don't like this, I want each bundle to be installed in the folder according Symbolic name like: xmlreader, code-analyzer etc.
Unfortunately it is not possible by setting it in Felix config properties. But I hope that there should be some ready solutions instead inventing the wheel.
How to implement this feature in proper way?