Maven can't find class even though package is included - java

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.

Related

Where am I going wrong in trying to import a class?

Here's the code for the source file:
package moa4;
public class Book {
....
}
And for the destination file:
import moa4.Book;
public class Library {
...
}
The source and the destination are both saved in the same directory with the address:
C:\Users\\java\M\moa4
I'm getting the following error: package moa4 does not exist
You asked Library to import a package moa4.Book but you defined no such package. Instead, you defined a type Book inside package moa4, and that is not consistent with your import directive.
You could either import the package, or make that an import static of the class, but since Book and Library are both in the same package you don't need the import directive at all.
As mentioned, C:/Users/java/M needs to be in your classpath ("-cp" option).

Java classes and packages

Hello everybody I am a beginner of Java. I am blocked at this point with the following
program:
import prog.io.Orario;
import prog.io.ConsoleOutputManager;
class primoprogramma{
public static void main(String[] args){
ConsoleOutputManager video=new ConsoleOutputManager();
video.println("ciao");
}
}
That gives me the error:
bad class file: ./prog/io/Orario.class
class file contains wrong class: prog.utili.Orario
Please remove or make sure it appears in the correct subdirectory of the classpath.
I did everything I could tried in those days but nothing works. Here there the class
Orario:
package prog.utili;
public class Orario {
private static char separaratore=';';
}
Thank you for any advice
Your class Orario has the wrong package declaration (package prog.utili; instead of package prog.io;)
The compiler scans your import of prog.io.Orario.
It searches for class Orario in a file Orario.class in directory prog/io.
The class found has the package prog.utili declared which is not the desired one - Error
In java, directorys are the same as package names.
So, a class Orario in the package prog.utili
have to be in a directory prog/utili instead of prog/io

Getting Error for package name in netbeans

I am using one .java file and i have given a package name as com.onlinmebank but netbeans displaying error at this package declaration line as Incorrect Package.
Following is the package declaration code.
package com.onlinebank;
import java.sql.*;
import java.util.*;
public class BankCommons{
//All Code Here
}
Can Anybody tell me why i am getting this error
All Java keywords are lower-case!
So, this should work:
package com.onlinebank;
import java.sql.*;
import java.util.*;
public class BankCommons {
//All Code Here
}
And keep in mind, that the file BankCommons.java must be placed in the directory com/onlinebank.
In package, p should be small not Package(no captial P).
Also in public class p should be small. Similarily for Import also.
Important note all java keywords are in lower cases

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;

"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