"Cannot find symbol" error when creating an instance of an object - java

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

Related

Weka Filter class is not found

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.

"gs" scheme missing from installed FileSystemProviders

I want to read a file from Google Cloud Storage using NIO as described in the SDK Javadoc and the package README. I think I'm missing something about triggering the installation of a new FileSystemProvider in the Maven configuration.
I created a simple-as-possible project to demonstrate. The highlights follow.
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>
<groupId>com.example</groupId>
<artifactId>gcs-nio</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<name>playground</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-nio -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-nio</artifactId>
<version>0.22.0-alpha</version>
</dependency>
</dependencies>
</project>
App.java:
public class App {
public static void main(String[] args) {
System.out.println("FileSystemProvider schemes:");
List<FileSystemProvider> fileSystemProviders = FileSystemProvider.installedProviders();
for (FileSystemProvider provider : fileSystemProviders) {
System.out.println(provider.getScheme());
}
// This fails; I want it to succeed.
Paths.get(URI.create("gs://dataflow-samples/shakespeare/kinglear.txt"));
}
}
Output:
$ mvn compile -q exec:java -Dexec.mainClass=com.example.App
FileSystemProvider schemes:
file
jar
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project gcs-nio: An exception occured while executing the Java class. Provider "gs" not installed -> [Help 1]

Azure DeviceTwinStatusCallBack - cannot find symbol

I am working with Microsoft Device Twins tutorial available here. Using maven 3.5.0 and java 1.8.0_144.
My service app is building without any errors, but the problem is with a device app.
After copy-pasting code from the tutorial and trying to build the project with command "mvn clean package -DskipTests" I receive this error:
cannot find symbol
[ERROR] symbol: class DeviceTwinStatusCallBack
[ERROR] location: class com.mycompany.app.App
There 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>simulated-device</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>simulated-device</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-device-client</artifactId>
<version>1.3.33</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
DeviceTwinStatusCallBack isn't available on Microsoft Azure documentation. Anyone has any idea?
Problem resolved, I just made my own class implementing IotHubEventCallback:
protected static class MyTwinCallback implements IotHubEventCallback
{
public void execute(IotHubStatusCode status, Object context)
{
System.out.println("IoT Hub responded to device twin operation with status " + status.name());
}
}
and my startDevice Twin method now looks like:
client.startDeviceTwin(new MyTwinCallback(), null, dataCollector, null);

Spring application doesn't work after building in jar

I have a simple Spring application:
Main class:
#SpringBootApplication
#EnableScheduling
public class SchedulerApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulerApplication.class, args);
}
}
And class for scheduled job
#Component
public class Executor {
private static final Logger log = LoggerFactory.getLogger(Executor.class);
private Integer jobCounter = 1;
#Scheduled(fixedDelay = 1000)
public void run() {
log.info("Start task (" + jobCounter + ")");
log.info("Stop task");
jobCounter ++;
}
}
This is 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>ru.alexeyzhulin</groupId>
<artifactId>scheduler</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>scheduler</name>
<description>Task scheduler</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
That is my project structure
It works fine under the IDE (IntelliJ IDEA), but when I compiled this code to jar file and run:
java -jar scheduler.jar
I got a long stack of errors like this
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [ru.alexeyzhulin.SchedulerApplication]; nested exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.f actories. If you are using a custom packaging, make sure that file is correct.
What have I missed?
Add the following element under your sprig boot maven plugin in pom.
<configuration>
<fork>true</fork>
<mainClass>Your-main-config-class</mainClass>
</configuration>
It seems that your SpringBootApplication class located in a wrong place.
It should be properly located. Usually it is in a root of your artifact package.
For example:
ru.alexeyzhulin.scheduler
ru.alexeyzhulin.scheduler.SchedulerApplication
ru.alexeyzhulin.scheduler.Executor
or
ru.alexeyzhulin.scheduler
ru.alexeyzhulin.scheduler.SchedulerApplication
ru.alexeyzhulin.scheduler.components
ru.alexeyzhulin.scheduler.components.Executor
And look at it:
Failed to process import candidates for configuration class

Can't get biojava to work in a Maven Netbeans application

I have a problem getting BioJava to work in a Netbeans RCP application, built using Maven. I've created a Maven module as a wrapper, including org.biojava.* and org.forester.* packages as public in the POM. Then, from another module I set the wrapper as a dependency, and use some of the basic examples from the BioJava cookbook for testing.
Whenever I try to instantiate some object of a class from BioJava, the application freezes and I have to kill it using the Windows task manager.
Here's the wrapper's pom file:
<?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>
<parent>
<groupId>nl.hecklab.bioinformatics</groupId>
<artifactId>Spider-parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>BiojavaWrapper</artifactId>
<version>4.1.0</version>
<packaging>nbm</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<useOSGiDependencies>true</useOSGiDependencies>
<publicPackages>
<publicPackage>org.biojava.*</publicPackage>
<publicPackage>org.forester.*</publicPackage>
</publicPackages>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.biojava</groupId>
<artifactId>biojava-alignment</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Here's some code I try to get to work. This is just a very coarse example, called from a button in a TopComponent. Input and output are just text fields.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Reader r = new Reader(new File("D:\\current\\fastafile.fasta"));
for (ProteinSequence a : r.getSequences()) {
input.append(a.toString());
}
Profile<ProteinSequence, AminoAcidCompound> profile = Alignments.getMultipleSequenceAlignment(r.sequences);
output.setText(String.format("Clustalw:%n%s%n", profile));
ConcurrencyTools.shutdown();
}
Here's the reader class:
public class Reader {
List<ProteinSequence> sequences = new ArrayList<>();
public Reader(File fastaFile) {
try {
FileInputStream inStream = new FileInputStream(fastaFile);
FastaReader<ProteinSequence, AminoAcidCompound> fastaReader
= new FastaReader<>(
inStream,
new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(),
new ProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet()));
LinkedHashMap<String, ProteinSequence> b = fastaReader.process();
sequences.addAll(b.values());
} catch (IOException ex) {
Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<ProteinSequence> getSequences() {
return sequences;
}
}
In the (Netbeans) IDE, the classes are found and used in autocompletion, and the project builds successfully, in each case indicating that principally the dependencies are set up correctly.
First of all check the wrapper module's manifest to see if all entries are correctly generated, especially since you define useOSGiDependencies==true. It could be that the biojava jars contain osgi headers and then you are not wrapping the jars in module, but declare a dependency on osgi plugin.
However locking of the app is weird, if there was something wrong with the runtime dependencies I would have expected an early 'unsatisfied dependencies' error. You might want to create a thread dump and check what's going on. Maybe you have a deadlock. Or since your action (jButton1ActionPerformed) is called from AWT, maybe the whole reading thing just takes time and your UI thread is locked.
I've done a lot of searching and found that the actual culprit is slf4j, that's used throughout BioJava.
I don't know why it freezes the platform application, but I'm able to cause my module to not install by creating a slf4j logger in it.
I've seen a solution online for a wrapper module, and it turns out it's enough to create a wrapper for org.slf4j:slf4j-api:x.y.z together with org.slf4j:slf4j-jdk14:x.y.z. Add org.slf4j.* to the public packages. Here's the POM:
<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>
<parent>
<groupId>group</groupId>
<artifactId>parent-project</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>slf4jwrapper</artifactId>
<packaging>nbm</packaging>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>nbm-maven-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<publicPackages>
<publicPackage>org.slf4j.*</publicPackage>
</publicPackages>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<useDefaultManifestFile>true</useDefaultManifestFile>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-api-annotations-common</artifactId>
<version>${netbeans.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
The wrapper should then be used in the BioJava dependent modules, but it should work for other modules depending on slf4j as well.

Categories

Resources