Trying to import a class found in a JAR - java

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;

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());

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.

Can't see class from within a package

I have a Java project built using IDEA and Maven. To make this question simple:
suppose part of the structure of the project is
src -> main -> java
In java folder there is a package called PAK, for example, which contains class A. Also there is class B in java folder without package.
The problem is when I'm trying next code
package PAK;
public class A {
private B variable;
}
compiler can't see class B but class B is public.
You need to import the class B, because it's not in the same package with A
package PAK;
import B;
public class A {
private B variable;
}
If classes are in the same package, you don't need to import them.

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

Java compiler can not find symbol - public class in same package

I've got a problem with a Main class not finding another class being public, in the same folder and the same package. Both classes are named as their files. Here is the part seeming to contain the problem:
The Interface:
package hanoi;
public interface Stack<E> {
...
}
The Over-Class:
package hanoi;
public class DefaultStack<E> implements Stack<E> {
...
}
The Used class:
package hanoi;
public class HanoiStack extends DefaultStack<HanoiDisk> {
public HanoiStack (int a){
for (int b = a; b > 0; b--){
HanoiDisk disk = new HanoiDisk(b);
this.push(disk);
}
}
...
}
Main Class:
package hanoi;
public class TowersOfHanoi{
HanoiStack stack1 = new HanoiStack(0);
HanoiStack stack2 = new HanoiStack(0);
HanoiStack stack3 = new HanoiStack(0);
...
}
File Directory (of both):
...\eclipse\Hanoi2\src\hanoi
Eclipse error: Main class could either not be found or not be loaded
(there is actually a main method in the main class, but the rest of the code gets very complicated and doesnt seem to be interesting right now)
Java Compiler error: could nor find symbol: class HanoiStack
Another hint: a friend of mine is working on the same project, seeming toi have declared the interesting part same as me but not having any issues.
Update: download link to the full program is here
Looks like either Eclipse playing up, or it cant compile the classes for some reason.
1) Clean the project in Eclipse. (Project -> Clean -> Clean all projects) Then restart Eclipse for good measure.
2) Check the folder where the project is configured to build is writable. To check what this is, view the project build path (right click -> Build Path -> Configure build path) under source tab check the output folder.
If neither of these help, could you provide more info where the main class is. E.g. is it in the TowersOfHanoi class?

Categories