java import user defined package - error: cannot find symbol - java

My directory structure is as follows:
/WorkingDirectory
MyCollection.java
/au/edu/au
/UserInterface
UserInterface.java
/Collection
Album.java
CDAlbum.java
DVDAlbum.java
CollectionFactory.java
Where Album.java is an interface implemented by CDAlbum.java and DVDAlbum.java. Each .java file has the appropriate
package au.edu.uow.UserInterface;
or
package au.edu.uow.Collection;
line.
In UserInterface.java I declare an ArrayList of type Album, which gives me compile errors of
.\au\edu\uow\UserInterface\UserInterface.java:9: error: cannot find symbol
private ArrayList<Album> myCollection;
^
symbol: class Album
location: class UserInterface
I am compiling from MyCollection.java, which has import statements for both packages.
what is causing this error? I tried adding
import au.edu.uow.Collection
to UserInterface.java, but to no avail.

You need either:
import au.edu.uow.Collection.*;
or:
import au.edu.uow.Collection.Album;
Using wildcard imports is more convenient but some consider this a bad practice. This is a matter of style; decide for yourself.
It is also considered good style for package names to be in all-lower-case, although this is not enforced by the compiler.

So, you folder structure looks like /au/edu/au & your trying to import from au.edu.uow
Apart from what JimN has suggested, I think you'll find that Java will complain that te package doesn't exist or the files in /au/edu/au are in the wrong package
Either change the directory structure or package structure to match each other

Related

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.

Java & setting classpath & localization of packages

I've got structure of catalogs:
/home/etc./studies/JAVA/pack/Print.java
/home/etc./studies/JAVA/Lab2/zad1/pkg/A.java
/home/etc./studies/JAVA/Lab2/zad1/B.java
A is a class in package "pkg"
Print is a class in package "pack"
B imports the packages "pkg" and "pack".
When I tried to compile B.java, I get an error:
B.java:4: error: cannot access A
public class B extends A{
^
bad class file: /home/etc./studies/java/A.class
class file contains wrong class: pkg.A
Please remove or make sure it appears in the correct subdirectory of
the classpath.
Is it possible to include that packages without reorganization structure of files ?
It's clearly saying that:
class file contains wrong class name: pkg.A
that means probably you declared class name as pkg. An instead of A.
if you declare the package names as correct like this
home/etc/studies/JAVA/Lab2/zad1
home/etc/studies/JAVA/Lab2/zad1/pkg,
/home/etc/studies/JAVA/Lab2/zad1/B.java
You won't get the compilation error.
package home.etc.studies.JAVA.Lab2.zad1;
import home.etc.studies.JAVA.Lab2.zad1.pkg.A;
public class B extends A {
'enter code here`
}
You cannotreference Classes in a default package. Put every class into a 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;

Java type cannot be resolved MIME type class

I know this is a common question asked but I've been searching and I've included the class into eclipse through the buildpath. I start to write the import statement and it autocompletes options for me so I know it's finding the class.
My problem is how come it's giving this error when I'm reading the docs and it says the constructor method is MimeUtil2() ?
http://www.jarvana.com/jarvana/view/eu/medsea/mimeutil/mime-util/2.1/mime-util-2.1-javadoc.jar!/eu/medsea/mimeutil/MimeUtil2.html#MimeUtil2()
package com.jab.app;
import java.io.File;
import eu.medsea.mimeutil.*;
public class CheckFileType {
private void GetMimeType(File filename){
MimeUtil2 test = new MimeUtil2(); //Produces the error saying java type cannot be resolved
}
I think you need to import
import eu.medsea.mimeutil.*;
According to the documentation, the type is eu.medsea.mimeutil.MimeUtil2
I ended up finding out that I was using the test-source.jar not the main jar file itself. The sourceforge page made the default as the source file instead of the main jar file.
It was buried inside of the files page.

"class, interface, or enum expected" error when trying to create package

I'm trying to declare a package in a file as follows:
import java.util.*;
package rtg;
public class Generate
{
// ...
}
But I'm getting an error when I try to compile this:
Generate.java:3: class, interface, or enum expected package rtg;
Why am I getting this error?
it should be
package rtg;
import java.util.*;
public class Generate{
}
In java you first define package then imports and then class. See wiki here: Java_package and Oracle's tutorial here: Java Packages
Edit
Now to call Genereate class from a class in same folder that is rtg folder:
package rtg;
public class GUI{
Generate gen = new Generate();
}
Make sure all words are spelled correctly.
The pacakge declaration must be the first thing in a Java file (apart from comments). You can't put the imports above it.
All the examples are above is good but we have to compile this package making class by swich standard ... You have to give "-d" and destinations folder for making package in it. "c: \f1 >javac -d e: \f2 temp . Java" 'c,e'are drive, 'f1,f2' are folder, temp is class name.

Categories