Java classes and packages - java

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

Related

Is their any class/classes inside java root package

The below code has no compilation error:-
import java.*;
class Test{
public static void main(String[] args){
}
}
My question is does the package named java only includes sub-packages or it also includes any class/classes. If yes then which class(s). If no then why we are able to import it.
There are no class directly under java. All the JDK's classes are under subpackages.
Having an empty package (or a package with no classeses in it) is perfectly legal in Java. You can import all the classes in it (which is no classes) with the * syntax. This isn't wrong - it's just pointless.

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.

How to import a custom package into another? Getting error package "packagename" does not exist

So right now in my code I have this:
package pokemonbattle;
import pokemon.Pokemon.*;
public class PokemonBattle { //Do stuff }
and then I also have:
package pokemon;
public class Pokemon {
public static void main(String[] args) {}
public String getName(int pokemon) {
//stuff
}
Except that I'm getting the error "Package pokemon.Pokemon does not exist." When it does. The file name it's under is called Pokemon, but I've tried using different capitalization but nothing is working. Any suggestions? Thanks!
Packages are like directories and you should put your files into the place specified by their package. In each directory can either be files (classes, interfaces, ..) or other sub directories.
When you do
package pokemon;
public class Pokemon {
You express that this is the Pokemon.java file in the \pokemon directory. In other words \pokemon\Pokemon.java.
To use that class in another place you do either
import pokemon.Pokemon;
to import just that one class specifically or you do
import pokemon.*;
to import all the classes in that package (but not sub-packages thereof) When you say pokemon.Pokemon.* it's looking for a directory named \pokemon\Pokemon\ which doesn't exist.

Trying to import a class found in a JAR

I have built a simple project called LibTest that has one class with the following code:
public class MainTest
{
public static tclass l;
}
In secondary simple project I have the defined class tclass:
public class tclass
{
int i;
}
Then I export tclass to a JAR file. At LibTest->Properties->BuildPath I click on AddExternalJar and select tclass.jar ( I also tried checking the JAR at Order and Export) but I still get an error at MainTest "tclass cannot be resolved to a type".
I don't see what is missing.
Thanks
Simon
After including your jar file, you also need to import your class using its full path in your MainTest class. e.g:
import com.package.tclass;

"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