Currently trying to work with objects in Java. Everything goes fine until I hit compile. Have been reading a couple of other questions regarding the same problem, or the same given error, and at this point I am not sure wether I am forgetting something or that I need to change my classpath.
Main Class file:
package TesterClass;
public class Tester {
public static void main(String[] args){
TesterClass firstTest = new TesterClass();
firstTest.stringPrinter();
}
}
The file that is supposed to be functioning as a package file:
package TesterClass;
public class TesterClass{
private String workingSegment;
public TesterClass(){
workingSegment = "Working";
}
public void stringPrinter(){
System.out.println(workingSegment);
}
}
The 2 files are in the same directory and I am trying to manually compile them with
"javac Tester.java". The error I get is about the fact that its having issues with the package. All help is welcome!
EDIT: Forgot to post the actual compiler error.
Tester.java:9: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
TesterClass firstTest;
^
Tester.java:11: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
firstTest = new TesterClass();
^
2 errors
Move to the top of the source tree and compile both class...
So, assuming you source files are in \Java\TesterClass, you need to start in \Java
javac TesterClass\Tester.java TesterClass\TesterClass.java
You may also want to have a quick read of Code Conventions for the Java Programming Language as packages names are suppose to be in lower case :P
Updated
I just tried...
javac TesterClass\Tester.java
And it worked fine.
Are you sure that the Tester.java and TesterClass.java are in the TesterClass directory?
Updated with running example
So, basically, I dropped you .java files into the directory \compile under the TesterClass (\compile\TesterClass) directory and compiled them using...
\compile>javac TesterClass\Tester.java
Then I run them...
\compile>java TesterClass.Tester
Working
You need to go to the top of the directory hierarchy and first compile your TesterClass and then compile your Tester. Since you have not compiled your TesterClass yet, Tester is unable to find it.
The error clearly states that its not able to find the symbol TesterClass, and the reason being TesterClass hasn't been compiled yet.
I suggest you use an IDE which does the compilation automatically for you. If you stick to manual compilation, you need to compile all the classes in the proper order.
Try changing the package name so it does not match the class name. Right now they are the same. Make it package TesterClassPackage, then import TesterClass into the file with the main() method. Even though they are in the same package sometimes you need to literally import files even though they are in the same package.
javac TesterClass\TesterClass.java TesterClass\Tester.java
will do it
Related
I'm trying to compile the following code (one of two files I need to complete this homework) but I'm getting 2 errors in cmd. This is what cmd throws at me:
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
CarRentalTest.java:12: error: cannot find symbol
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
^
symbol: class CarRental
location: class CarRentalTest
2 errors
And this is the code I'm trying to compile.
public class CarRentalTest {
public static void main (String[] args)
{
CarRental myCarRental = new CarRental(); //create CarRental object CarRental
myCarRental.Customers();
} //end method main
} //end class CarRentalTest
What's weird is that the whole thing runs fine in NetBeans. What am I doing wrong here? :9
What am I doing wrong here?
Not building CarRental, or not telling the compiler where to find the class if you have already compiled it. The IDE is probably assuming you want to build everything, so that's fine.
We don't know how your code is organized, but you should either pass all the relevant filenames to the compiler at the same time:
javac -d classes src\CarRental.java test\CarRentalTest.java
... or put the output directory of the earlier compilation in the classpath for the later compilation, e.g.
javac -d classes src\CarRental.java
javac -d testclasses -cp classes test\CarRentalTest.java
If you are using a standard directory layout for your project, where production and test code are in separate directory trees then the java command line will not see the production class if your currect directory is the test directory.
To clarify:
Suppose you have this dir structure:
src/
main/
java/
mypackage/
CarRental.java
test/
java/
mypackpage/
CarRentalTest.java
and you are in the 'src/test/java/mypackage/' directory, you would experience this error when running javac at the command line - although the production and test classes are in the same package, they are in different directories.
The IDE knows about this directory structure, includes the test path during compilation and therefore it works OK.
You need to import CarRental class in the CarRentalTest.
import yourpackage.CarRental in the CarRentalTest. Java Compiler can't find the CarRental in the CarRentalTest.java.
In the IDE whole package comes in the java file
import package.car.*;
This is why it is working in IDE.
Trying to learn Java again and I cannot remember how I figured this out the first time around.
I have 3 classes; a GameLauncher, GuessGame, and Player. GameLauncher has my main method.
They are all packaged as chap02, I cannot remember if that is important to me yet.
I am compiling like this: javac GameLauncher.java GuessGame.java Player.java
running like this: java GameLauncher
I am getting this error: Could not find or load main class GameLauncher.
I know this is a ridiculous issue, but I have always had trouble with this kind of stuff. The actual programming and writing code I can pick up just fine, but dealing with these damn compilers always gets me. Any help would be appreciated. Thanks
package chap02;
public class GameLauncher {
public static void main (String[] args) {
GuessGame game = new GuessGame();
game.startGame();
}
}
Edit: The issue isn't with the actual code, the issue is with how I am compiling it.
When running the program, you have to run it form the correct directory. Remember, that you used packages, so you have the following package-structure
src
chap02
GameLauncher.java
GuessGame.java
Player.java
after compilation, you will find the corresponding .class-files in the chap02-folder. To start the game, you have run the following command from the src-directory:
java chap02.GameLauncher
Since you specified a package in the source code, you have to use the full qualified name, including the package, to which the class belongs.
EDIT as vefthym mentioned, you have to compile the code in the same way, running
javac chap02/GameLauncher.java
from the source-directory.
EDIT 2
"src" is the directory, where your src lies. I, for example, have my Code unter X:\JDK-Projects[Project-name]\src. You have to specify the full absolute path to the src-Directory or the relative path form the directory you are currently in.
I have written a code in java. In which I have created a package called xml-creator.
Package xml_creator has 3 classes say XML_Control, XML_Creator, and XML_implement.
When I run my project on netbeans (NetBeans 7.0) it works fine. But if I try to compile code on console, I get various errors like
When I compiled XML_Creator.java, I get following errors.
XML_Creator.java:371: cannot find symbol
symbol : variable XML_implement
location: class xml_creator.XML_Creator
typeAttr.setValue(XML_implement.table_col[i][2]);
^
XML_Creator.java:375: cannot find symbol
symbol : variable XML_implement
location: class xml_creator.XML_Creator
for(int j=0;j<XML_implement.kTab;j++)
^
XML_Creator and XML_implemenr both are in same package but non of them extend each other.
I am sorry I cant show code on this site as it is too large and aginst the company's policies.
I dont understand why it is showing me errors?
Sample code
XML_Control.java
package xml_creator;
public class XML_Control
{
public static void main(String as[])
{
XML_Creator xml = new XML_Creator();
}
}
XML_Creator.java
package xml-creator;
public class XML_Creator
{
XML_implement ixml = new XML_implement();
public XML_Creator()
{
System.out.println(""+ixml.a);
}
}
XML_implement.java
package xml_creator;
public class XML_implement
{
public int a;
public XML_implement()
{
a = 10;
}
}
So when I compile XML_Creator.java, console gives error.
It sounds like you're compiling within the directory containing the .java file, and only telling the compiler about one of the source files. That's the problem - to try to find a source or class file, the compiler is using the package name, and expecting the packages to be laid out in the conventional fashion. Compile from the root of the source tree - which I certainly hope you're using - like this:
javac xml_creator/*.java
You may also want to specify an output directory - which again will be the root of the directory hierarchy for packages:
javac -d bin xml_creator/*.java
If you're building regularly from the command-line (and not just for throwaway code) you should look into using a build system such as Ant.
I am new to java and to the eclipse IDE.
I am running Eclipse
Eclipse SDK
Version: 3.7.1
Build id: M20110909-1335
On a windows Vista machine.
I am trying to learn from the book Thinking in Java vol4.
The author uses his own packages to reduce typing. However the author did not use Eclipse and this is where the problem commes in..
This is an example of the code in the book.
import java.util.*;
import static net.mindview.util.print.*;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
print("this does not work");
}
this is the contents of print.Java
//: net/mindview/util/Print.java
// Print methods that can be used without
// qualifiers, using Java SE5 static imports:
package net.mindview.util;
import java.io.*;
public class Print {
// Print with a newline:
public static void print(Object obj) {
System.out.println(obj);
}
// Print a newline by itself:
public static void print() {
System.out.println();
}
// Print with no line break:
public static void printnb(Object obj) {
System.out.print(obj);
}
// The new Java SE5 printf() (from C):
public static PrintStream
printf(String format, Object... args) {
return System.out.printf(format, args);
}
} ///:~
The error I get the most is in the statement.
Import static net.mindview.util.print.*;
On this staement the Eclipse IDE says it cannot resolve net
also on the
print("this does not work");
The Eclipse IDE says that the class print() does not exist for the class HelloWorld.
I have been trying to get these to work, but with only limited success, The autor uses another 32 of these packages through the rest of the book.
I have tried to add the directory to the classpath, but that seems to only work if you are using the JDK compiler. I have tried to add them as libraries and i have tried importing them into a package in a source file in the project. I have tried a few other things but cant remember them all now.
I have been able to make one of the files work, the print.java file I gave the listing for in this message. I did that by creating a new source folder then making a new package in that foldeer then importing the print.java file into the package.
But the next time I try the same thing it does not work for me.
What I need is a way to have eclipse load all these .java files at start up so when I need them for the exercises in the book they will be there and work for me, or just an easy way to make them work everytime.
I know I am not the only one that has had this problem I have seen other questions about it on google searches and they were also asking about the Thinking In Java book.
I have searched this site and others and am just not having any luck.
Any help with this or sugestions are welcome and very appreciated.
thank you
Ok I have tried to get this working as you said, I have started a new project and I removed the static from the import statement, I then created a new source folder, then I created a new package in the source folder. Then I imported the file system and selected the the net.mindview.util folder.
Now the immport statement no longer gives me an error. But the the print statement does, the only way to make the print statement work is to use its fully qualified name. Here is the code.
import net.mindview.util.*;
public class Hello2 {
public static void main(String[] args) {
Hello2 test = new Hello2();
System.out.println();
print("this dooes not work");
net.mindview.util.Print.print("this stinks");
}
}
The Error on the print statement is:
The method print(String) is undefined for the type Hello2
and if I try to run it the error I get is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method print(String) is undefined for the type Hello2
at Hello2.main(Hello2.java:6)
The Statement::::: net.mindview.util.Print.print("this stinks") is the fully qualified print statement and it does not throw an error but it does totally defeat the purpose of the print.java file..
If you have any questions please ask Ill get back to you as soon as I can.
I've had similar issues. I solved it by following the steps below:
Click File->New->Java Project. Fill in UtilBuild for the ProjectName. Chose the option "Use project folder as root and click 'Finish'.
Right-click on UtilBuild in the PackageExplorer window and click New->package. For the Package Name, fill in net.mindview.util
Navigate within the unzipped Thinking In Java (TIJ) folder to TIJ->net\mindview\util. Here you will find all the source code (.java) files for util.
Select all the files in the net\mindview\util folder and drag them to the net.mindview.util package under UtilBuild in Eclipse. Chose the 'Copy Files' option and hit 'OK'.
You will probably already have the 'Build Automatically' option checked. If not, go to Project and click 'Build Automatically'. This will create the .class files from the .java source files.
In Eclipse, right-click on the project you were working on (the one where you couldn't get that blasted print() method to work!) Click Properties and Java Build Path->Libraries. Click 'Add Class Folder...' check the box for UtilBuild (the default location for the .class files).
I think the confusion here arises due to CLASSPATH. If you use Eclipse to build and run your code then Eclipse manages your CLASSPATH. (You don't have to manually edit CLASSPATH in the 'Environment Variables' part of your computer properties, and doing so changes nothing as far as Eclipse Build and Run are concerned.)
In order to call code that exists outside your current project (I will name this 'outside code' for convenience) you need to satisfy three things:
A. You need to have the .class files for that code (as .class files or inside a JAR)
B. You need to indicate in your source code where to look for the 'outside code'
C. You need to indicate where to start looking for the 'outside code'
In order to satisfy these requirements, in this example we:
A. Build the project UtilBuild which creates the .class files we need.
B. Add the statement import static net.mindview.util.Print.*; in our code
C. Add the Class Folder library in Eclipse (Java Build Path->Libraries).
You can investigate the effect of Step C by examining the .classpath file that lives directly in your project folder. If you open it in notepad you will see a line similar to the following:
<classpathentry kind="lib" path="/UtilBuild>
You should combine this with your import statement to understand where the compiler will look for the .class file. Combining path="/UtilBuild" and import static net.mindview.util.Print.*; tells us that the compiler will look for the class file in:
UtilBuild/net/mindview/util
and that it will take every class that we built from the Print.java file (Print.*).
NOTE:
There is no problem with the keyword static in the statement
import static net.mindview.util.Print.*;
static here just means that you don't have to give specify the class name from Print.java, just the methods that you want to call. If we omit the keyword static from the import statement, then we would need to qualify that print() method with the class it belongs to:
import net.mindview.util.Print.*;
//...
Print.print("Hello");
which is slightly more verbose than what is achieved with the static import.
OPINION:
I think most people new to Java will use Eclipse at least initially. The Thinking in Java book seems to assume you will do things via command line (hence it's guidance to edit environment variables in order to update CLASSPATH). This combined with using the util folder code from very early in the book I think is a source of confusion to new learners of the language. I would love to see all the source code organised into an Eclipse project and available for download. Short of that, it would be a nice touch to include the .class files in just the 'net/mindview/util' folder so that things would be a little easier.
U should import package static net.mindview.util not static net.mindview.util.Print
and you should extend the class Print to use its method.......
You should remove the static keyword from your import decleration, this: import static net.mindview.util.print.*; becomes this: import net.mindview.util.print.*;
If that also does not work, I am assuming you did the following:
Create your own project;
Start copying code directly from the book.
The problem seems to be that this: package net.mindview.util; must match your folder structure in your src folder. So, if your src folder you create a new package and name it net.mindview.util and in it you place your Print class, you should be able to get it working.
For future reference, you should always make sure that your package decleration, which is at the top of your Java class, matches the package in which it resides.
EDIT:
I have seen your edit, and the problem seems to have a simple solution. You declare a static method named print(). In java, static methods are accessed through the use of ClassName.methodName(). This: print("this dooes not work"); will not work because you do not have a method named print which takes a string argument in your Hello2 class. In java, when you write something of the sort methodName(arg1...), the JVM will look for methods with that signature (method name + parameters) in the class in which you are making the call and any other classes that your calling class might extend.
However, as you correctly noted, this will work net.mindview.util.Print.print("this stinks");. This is because you are accessing the static method in the proper way, meaning ClassName.methodName();.
So in short, to solve your problem, you need to either:
Create a method named print which takes a string argument in your Hello2 class;
Call your print method like so: Print.print("this stinks");
Either of these two solutions should work for you.
In my case I've dowloaded and decompressed the file TIJ4Example-master.zip. in eclipse workspace folder. The three packages : net.mindview.atunit, net.mindview.simple and net.mindview.util are in this point of the project :
and java programs runs with no problems (on the right an example of /TIJ4Example/src/exercises/E07_CoinFlipping.java)
I have two Java classes "giveMyOb" and "dataConn" declared in the same directory. Both are public classes. "giveMyOb" has a static method "getMine()". Inside dataConn, I called the static method as
giveMyOb.getMine();
When I try to compile dataConn.java, the following error is returned.
"Cannot find symbol
symbol: variable giveMyOb
location : class dataConn
giveMyOb.getMine(); "
It used to work earlier. But is not working now. Why is that?
Additional Information: JDK 1.6. Windows 7. 64 bit.
Update(30 days after the question): When compiled from Eclipse, the classes are referenced and it works. But the same won't work when compiling from command line. I was unable to figure out the reason and nothing logical comes to my mind!
javac -classpath . *.java
ought to create both .class files at the same time. It's more complicated by packages. I'm assuming you have none.
Learn the Sun Java coding conventions. You aren't following them with those class names. They should start with a capital letter.
Try this:
giveMyOb.java
public class giveMyOb {
public static String getMine() {
return "Yay, it works!";
}
}
dataConn.java
public class dataConn {
public static void main(String[] args) {
System.out.println(giveMyOb.getMine());
}
}
Then compile it all:
javac *.java
and run the main class:
java -cp . dataConn
// output: Yay, it works!
Note that Java's coding conventions recommend class names start with a capital.
If "it" still doesn't work, try removing the .class files manually then recompile again.