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?
Related
I have a problem:
I have to create a project for my studies. In the requirements I have a information that the whole project should be created in two files A.java and B.java.
The problem of project is complicated so I have created some helpfull class called e.x. MyClass. I put this class inside A.java - I cannot use another file.
Right now my A.java looks like that:
public class A {
// some logic
}
final class MyClass implements Comparable<MyClass> {
//some logic
}
In the B.java I am using objects of MyClass and everything is working correctly.
The problem is with the second requirement of project.
The automatic compilation - after I will send my project to my university - starts the proccess like that:
javac –Xlint B.java A.java
Compilation is successfull but I have some warnings:
B.java:17: warning: auxiliary class MyClass in A.java should not be accessed from outside its own source file
private MyClass variable;
The third requirement says that if you have any warnings or errors, your project will not be assessed. So I will fail my project if the warnings appears.
I know this is very stupid to store two classes in one file but this is unversity - here everything is stupid...
So - is there any solution to turn off warnings during compilation with javac?
I tried this:
#SuppressWarnings("all")
final class MyClass implements Comparable<MyClass> {
but it doesn`t work - I still get the warnings...
Any ideas? :)
Put MyClass inside A:
public class A {
// some logic
static final class MyClass implements Comparable<MyClass> {
//some logic
}
}
and refer to it as A.MyClass from the other file.
Or import it (assuming A isn't in the default package), and refer to it as MyClass.
Or defining it in its own file would be the obvious option; but you say you can't do that.
I am learning how to use packages in Java, but I am running into trouble when trying to implement them. I have a simple class called Main which appears as follows:
public class Main
{
public static void main(String[]args)
{
System.out.println("Package Test...");
}
}
The directory of this class is: C:\Users\MyComputer\Desktop\Packages\Main.java
When I compile this class, I run into no trouble. However, when I add "package com.example.mypackage;" to the top of the .java file, compile the program, and try to run the program, I receive the following error: "Error: Could not find or load main class Main"
What can I do to solve this problem?
If the path of your class is C:\Users\MyComputer\Desktop\Packages\Main.java then your class is not in a package. In this instance "Packages" is your project folder, and it only contains the one java class.
If you want package com.example.mypackage; to work, then your path needs to be:
C:\Users\MyComputer\Desktop\Packages\com\example\mypackage\Main.java
I'm using Eclipse and I have two different projects: A and B.
In project A, I have a class classA where I need to call a method methodB() from a class classB contained in the project B, how can I do that?
I've tried adding the project B to project A build path, but still doesn't work.
Thanks.
You need to add another project in "Project" tab, or add class folder of the project in "Libraries" tab ie you may try to add project B to the Run configuration used by project A. Go to the menu Run -> Run configurations, ther you can add the project B in the tab 'classpath' of your run configuration.
Here's an example that you may find helpful:
Project_1 has the following class:
ClassProjectOne.java which consists of:
public class ClassProjectOne {
private int m_Age;
private final int AGE_INPUT = 15;
public ClassProjectOne() {
setAge(AGE_INPUT);
}
public int getAge() {
return m_Age;
}
private void setAge(int age) {
m_Age = age;
}
}
Project_2 has the following class:
ClassProjectTwo.java which consists of:
public class ClassProjectTwo {
public static void main(String[] args) {
ClassProjectOne t = new ClassProjectOne();
System.out.println(t.getAge());
}
}
In order for this to work, you must right click Project_2 and click on Properties. Then click on Java Build Path -> Add... -> Select Project_1 -> OK. This sets a Java Build Path.
If your class is static there is no need to initialize a new instance of it.
Hope this helps.
I've just done what you're trying to do. I called my first project 'project1'. In this projects i have a package called 'package1' which in turn contains a class called 'Class1' containing a (public) static method called 'staticMethod'. I called my second project 'project2' with a class 'Class2' in 'package2'. I added project1 to the build path of project2 and then inserted the statement import package1.Class1 at the beginning of the class Class2.
Put the Project B on the Build path, then do a Clean project from Project Menu option and then use it.
Click in "A" --> Properties --> Build Path --> Projects ---> Add the Project ---> Ok
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;
I'm not new to Java but I never learned Packages. Anyways, let's say I create a folder called maina.
I put in file main.java in folder maina:
package maina;
import other.Dice;
public class main
{
public static void main(String[] args)
{
System.out.println("Hello world!");
System.out.println(Dice.test);
}
}
Then I create a new folder called other inside the folder maina. In folder other I put file Dice.java:
package other;
public class Dice
{
public Dice() {
String test = "Testing!";
}
}
OK, now Dice.java compiles fine.
However when I compile main.java I get this error:
C:\Users\tekaffi\Documents\ask\maina\main.java:13: cannot find symbol
symbol : variable test
location: class other.Dice
System.out.println(Dice.test);
^
1 error
Tool completed with exit code 1
What am I doing wrong?
Here's the error I get when I compile:
C:\Users\wekaffi\Documents\ask\maina\myMain.java:3: package maina.other does not exist
import maina.other.Dice;
^
C:\Users\wekaffi\Documents\ask\maina\myMain.java:13: cannot find symbol
symbol : class Dice
location: class maina.myMain
Dice myDice = new Dice();
^
C:\Users\wekaffi\Documents\ask\maina\myMain.java:13: cannot find symbol
symbol : class Dice
location: class maina.myMain
Dice myDice = new Dice();
^
3 errors
Tool completed with exit code 1
It has nothing to do with packages.
Your code is seriously messed up, you're trying to call a "test" member on the "Dice" class but you haven't created that member. besides that, you can't have a class named "main" and then have a static main method in it beacuse the compiler will think the main method is the constructor you need to rename your class to something else.
For your code to work your Dice class needs to look like this:
package maina.other;
public class Dice
{
public String test;
public Dice() {
this.test = "Testing!";
}
}
And for the print to work you need to create a new instance of Dice before you print
Either that or you make Dice static. So your main needs to be renamed to myMain and then the class should look like this:
package maina;
import maina.other.Dice;
public class myMain
{
public static void main(String[] args)
{
System.out.println("Hello world!");
Dice myDice = new Dice();
System.out.println(myDice.test);
}
}
If you're placing stuff where you said you are, it should work fine package-wise
Your Dice class must have a package declaration like so
package maina.other;
Your main class should import Dice like so
import maina.other.*;
It'd be package maina.other, if it is in /maina/other
Dice needs a public static string test. The current test is a non-static local variable to the constructor. Or you can make test non-static and then have the constructor set the test member and then do new Dice().test in the main.java
And the package name doesn't matter, foldering is only a convention and is IIRC ignored by the compiler. So thats not the issue here!
When you put package 'other' inside of 'maina' the new package is
package maina.other;
package maina.other;
As a side note, if you're using an IDE like Eclipse, you don't have to make the directories manually - it does it for you. Also navigating your packages with the package viewer is easy.
This one is tough to explain, and you've mentioned that you're new to Java, so please don't let me confuse you.
The package of a top-level type, such as main or Dice is whatever is listed in the package declaration. The package for Dice could just be other, even though the corresponding directory is nested inside the directory that corresponds to package amain.
The key is resource discovery when compiling and running. When you compile, you can specify a sourcepath and a classpath that helps the compiler resolve dependencies. Likewise, when you run, you can specify a classpath that helps the JVM resolve dependencies. There is no restriction that root packages not be nested inside one another. So, both amain and other could be root packages, like so:
% cd <directory-that-contains-amain>
% javac -sourcepath .:amain amain/main.java amain/other/Dice.java
% java -classpath .:amain amain.main
This is considered abnormal (and consequently poor) practice, however. You shouldn't do it, but you could.