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.
Related
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
Q) With respect to accessing static content, what is the difference
between user defined package and java system package (say java.lang
etc)
I'm preparing for ocjp6. using 1.6.26 java version
my java program has a package named "pack" in PackageTest.java
package pack;
public class PackageTest {
public static final int i=20;
}
}
javac -d . PackageTest.java
created PackageTest.class file in pack folder
now accessing static contents of PackageTest class from another java
program (TestStaticContents.java) as below
import pack.PackageTest.*;
// here importing all contents of PackageTest class
class TestStaticContents {
public static void main(String[] args){
System.out.println("normal import, accessing i value with class name: "+PackageTest.i);
}
}
javac TestStaticContent.java
displaying Compilation Error:
TestStaticContents.java: cannot access PackageTest bad class file: .\PackageTest.java
If i try accessing static contents of Math class from my java program
its not displaying any compilation error i.e
import java.lang.Math.*;
// here importing all contents of Math class
class TestMathStaticContents {
public static void main(String[] args){
System.out.println("normal import, accessing pi value with class name : "+ Math.PI);
}
}
javac TestMathStaticContents.java
No Compilation Error, and PI value is printed as expected.
Why this behavior is different compared to User defined package?
I believe I understand your problem now. You've placed both PackageTest.java and TestStaticComponents.java in the same package, pack. Two classes that share the same package cannot explicitly import one another.
You've hinted at this yourself by providing TestStaticComponents.java with a package-private class identifier.
Your import statement works perfectly if both classes reside in different packages.
import java.lang.Math.* attempts to import the nested types from Math, not the static members. You should be using import static:
import static java.lang.Math.PI; //recommended to avoid wildcards
When using imported static members, you do not need to reference the class when using the member: You can use PI instead of Math.PI.
No Compilation Error, and PI value is printed as expected.
This is because java.lang.Math, like all types in the java.lang package, is automatically imported: there's no need to import it. Because of this, Math.PI isn't causing an error like you'd expect.
If you used PI instead of Math.PI, you would have gotten an error, informing you the static member wasn't imported.
So i'm awfully new to coding but i quite like it, i'm really young so i have 0 experience on related stuff.
I'm watching this youtube series about java code and in this episode:
he creates another class and uses it in the main one but im on intelij(not eclipse as he is) and it gave me two errors saying java couldnt find the symbol (my second class);
my code:
package com.company;
public class Main {
public static void main(String[] args) {
tuna tunaObject = new tuna();
tunaObject.simpleMessage(null);
}
Second class:
public class tuna{
public void simpleMessage(){
System.out.println("Another class");
}
}
Your simple message method does not accept parameters, so don't try to pass in any. Instead of calling simpleMessage(null) simply call simpleMessage().
Also either make sure that the tuna class is located in the same package as your main class, or import the tuna class via an import statement above the Main class and below the package declaration. Even if the two source files are in the same physical directory, the Java compiler won't understand which class you are referring to unless you specifically define each class in the same package.
Adjust your second class to:
package com.company;
public class tuna{
public void simpleMessage(){
System.out.println("Another class");
}
}
Wecome to Java.
Maybe you can confirm first that the second class is located in the same package with the Main. And it is better to claim a class in First letter upper-cased format.
I am implementing the following sample interface:
package test1;
public interface MotorVehicle {
void run();
int getFuel();
}
In the class
package test1;
import test1.MotorVehicle;
public class Car implements MotorVehicle
{
int fuel;
public void run(){
System.out.println("Running");
}
public int getFuel(){
return this.fuel;
}
}
When I try to compile the class file , I get the following error :
Car.java:4: error: cannot find symbol
public class Car implements MotorVehicle
^
symbol: class MotorVehicle
1 error
Compile Steps:
Step:1 javac MotorVehicle.java
Step:2 javac Car.java
Both my interface and the class are in the same directory , why does ut come up with cannot find symbol error?
Edit:
As suggested , have changed the package , and tried to run the same code again . Still getting an error.
The problem is that you're in the wrong folder when compiling.
From the console screenshot, it is clear that you are inside /test1. However, the package test1; statement expects a folder inside the current folder named test1. It can't find that folder/package, so you get an error.
The solution is to go up one folder, so you end up in /src, then compile using the path to the file, e.g. javac test1/Car.java. Explanation: You are in the folder /src, the package statement inside the classes says they are inside the folder test1 which is inside /src. Now every package/path can be resolved.
And you shouldn't import things that are in the same package.
First of all as your package name is test you must keep your class and the interface in a folder named test.
Second thing since they are in the same folder named test remove import test.MotorVehicle; from the class defination
Suppose if your folder test resides in g:/ such that g:/test/contains class and the interface.
Then try opening the command prompt in g:/
then type the following commands
for compiling
javac test/Car.java
and for executing
java test.Car
Though you may get Error: Main method not found in class test.Car
as your class does not contain main mathod
You are going in to exact path by the use of cd command.Because of that interface is not accessible as class will try to find out it from package from current/running location.
For make this compile you have to specify fully (again Fully) qualified name of package during compilation.
For Example
If you class is in a.b.test package compile it like this
javac a/b/test/Car.java
First compile MotorVehicle as it doesn't have any dependencies. Then set the classpath
Before issuing javac Car.java compile statements you need to set the Classpath
Windows
set CLASSPATH=%CLASSPATH%;<PATH_TO_COMPILED_BINARY>/
Unix
export CLASSPATH=$CLASSPATH:<PATH_TO_COMPILED_BINARY>/
<PATH_TO_COMPILED_BINARY> should not include the package test1
Example :
C:/sourcecode/test1
Then <PATH_TO_COMPILED_BINARY> should be C:/sourcecode
Update
Removing the import test1.MotorVehicle will also fix the issue.
After Compiling Motorvehicle.java. you have to create a folder test1 and transfer the MotorVehicle.class into the folder test1 then compile the next file Car.java. This will solve your error
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