In java, why can I only import abstract classes? - java

In my Java I have a class import:
import cc.hyperium.mods.HyperiumModIntegration;
however, it fails to import with error Cannot resolve symbol 'HyperiumModIntegration'.
The class I'm importing looks like this:
package cc.hyperium.mods;
public class HyperiumModIntegration {
public HyperiumModIntegration() {
}
}
Weirdly, if I make the class abstract, it imports just fine.
IntelliJ will show the class in code completion, however.

Invalidating caches and restarting in IntelliJ fixed it.
(Note to self: stop using EAP.)

Related

Gradle (Java) cannot find symbol for class in subdirectory of package

It's been some years since I have programmed Java and I am having trouble with resolving a build issue with not finding a symbol for a class in the same package directory structure.
package com.A.B.C;
public class Manager {
...
}
The following class is attempting to reference Manager. Note the package declarations for both classes.
package com.A.B;
import com.A.B.C.Manager; // I have also tried omitting this import statement; same result
public class MyApp extends Application {
...
#Override
public void onCreate() {
...
registerActivityLifecycleCallbacks(Manager); // cannot find symbol SessionManager
}
...
}
I am importing com.A.B.C.Manager in other source files and able to reference Manager there but, for this source file, Gradle cannot resolve the symbol.
What am I missing? (This is an Android project by the way, in case that is relevant.)
In advance, thank you.
That Manager class have to implement Application.ActivityLifecycleCallbacks like this:
package com.A.B.C;
public class Manager implements Application.ActivityLifecycleCallbacks{
...
}
Also, I guess you missed the new while calling registerActivityLifecycleCallbacks() :
registerActivityLifecycleCallbacks(new Manager());

Maven can't find class even though package is included

I have the following code:
package osu.cs362.URLValidator;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
import static org.mockito.Mockito.*;
public class DomainValidatorTest {
RegexValidator rev = mock(RegexValidator.class);
}
This includes the package osu.cs362.URLValidator which contains RegexValidator.
However, when running mvn test I get:
cannot find symbol class RegexValidator
Why can't it find this class? Is this a pom.xml issue?
It is not a Maven problem.
If the RegexValidator class had the declaration like that :
package osu.cs362.URLValidator;
public class RegexValidator {
...
}
you would have not the problem. So I suppose it is not the case.
Besides, filesytem folders are not Java packages.
For example, nobody prevents you from declaring your class in the folder :
osu/cs362 of your classpath folder and declaring the package of the class like that: fictive.folder.
The class will compile.
It is the case for DomainValidatorTest. The package is not symmetric with the folder layout but the class is found by the compiler and it doesn't cause a compilation error.
But of course, it is a bad practice and it is misleading. That's why packages should always be symmetric to the folders layout.
You shoud move your DomainValidatorTest.java to directory:
src/test/java/osu/cs362/URLValidator
Directory structure should be the same as java package.

Using classes in my project that originate in a jar file

I'm using Eclipse on a Windows 7 64x machine. I've researched this problem and found many have had a similar one, but no solution I came across quite worked for me.
I'm working on a Project named Assignment_1, on a class named Percolation. I'd like to use the object WeightedQuickUnionUF which is inside a package contained within a jar file, named algs4.jar.
I seem to have added the Jar file I'm interested in to the build-path (it now appears under "Referenced Libraries"). The jar file algs4.jar resides in a folder named lib inside my project's folder.
However, when I try to declare an object of type WeightedQuickUnionUF inside my class, I get an error "WeightedQuickUnionUF cannot be resolved to a type".
I tried various import commands (including just import WeightedQuickUnionUF )before the class declaration and all of them yield the error "The import so and so cannot be resolved".
For example, this piece of code yields both of these errors. One at the import line, and another at the declaration of the WeightedQuickUnionUF object:
package assignment_1_package;
import algs4.WeightedQuickUnionUF;
public class Percolation {
private int[][] grid;
public int gridDimension;
private int opensGrid[][];
private WeightedQuickUnionUF model;
... //rest of class body here
This has baffled me for an entire day and I can't seem to figure this out. Thanks for your efforts.
Edit: here is a link to the class I wish to import: http://algs4.cs.princeton.edu/15uf/WeightedQuickUnionUF.java.html
Assuming you are talking about the algs4.jar of the class http://algs4.cs.princeton.edu/code/ , your import is incorrect you should do :
import WeightedQuickUnionUF;
BUT it's never a good idea to have class in the default package and it's actually not allowed to import a type from the unnamed package: this gives a compilation error.
http://docs.oracle.com/javase/specs/jls/se5.0/html/packages.html#7.4.2:
A type-import-on-demand declaration (§7.5.2) imports all the
accessible (§6.6) types of a named type or package as needed. It is a
compile time error to import a type from the unnamed package.
So in your case to solve your issue just create your classes in the default package so you don't have to do the import at all.
I'm in the same class, had the same problem. Removing my equivalent to these two statements
package assignment_1_package;
import algs4.WeightedQuickUnionUF;
resolved the problem. That's to say the following now resolves correcly
private WeightedQuickUnionUF model;
In my case, it helped adding
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.StdStats;
import edu.princeton.cs.algs4.WeightedQuickUnionUF;

Can not find test class 'junit.test.DepartmentTest' in project 'jyxxw'

when I run configurations, but my Eclipse shows that
Can not find test class 'junit.test.DepartmentTest' in project 'jyxxw'
This is a message that Eclipse gives when it doesn't recognize the class as being a test class. Do you have any #Test annotations in your class? (I realize that sounds like a silly question, but they could be in the superclass. In which case Eclipse doesn't see them.)
Can you try replacing your test code temporarily with a really simple test and see if the error goes away?
package junit.test;
import org.junit.*;
public class DepartmentTest {
#Test public void myTest() {}
}
If this works, you can see what's missing in your class. If it doesn't work, it is likely a classpath issue.

Permanently hidden warning from Scalac parsing Java code - compiler bug?

The scalac Java parser is taking objection to my Java code
imported `Entity' is permanently hidden by definition of object Entity in package domain Asset.java
This seems to be a collision between an import and a class with the same name in the package being compiled.
In my package I have a class
package iMP2020.domain;
public interface Entity {
public Serializable getId();
}
with the same name as an imported class from a different package
package iMP2020.domain;
import javax.persistence.Entity; // compiler warning
#Entity
public class Asset {
where it is complaining about the import. Javac is quite happy. Note that I don't have to reference my version of the class- just its existence is enough to trigger the warning on the import.
I can fix this by removing the import and explicitly referencing #Entity, but is it a bug in the compiler?
I don't seem to be able to reproduce this except with the Scala Eclipse plugin, so I'm going to wait for that to stabilise before coming to a conclusion.
You have two Entity references, one for your interface, and another one for javax.persistence.Entity.
Try to replace the second one with the full qualified name, removing the import:
package iMP2020.domain;
public interface Entity {
public Serializable getId();
}
and
package iMP2020.domain;
#javax.persistence.Entity
public class Asset {
I don't think it is a bug.
It doesn't make sense for an import to have the same name as a package member.

Categories