Results 1 to 8 of 8
Thread: confused with compiler
- 12-18-2009, 02:56 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
confused with compiler
I am using both windows and ubuntu linux when trying to compile .java files. when using windows there is no error and will create the class files, but when using linux there are errors which looks like it is having problems with the creating instances of a class when both java files are in the same folder and it works fine compiling one of the java files on the linux side but both on windows.... why would this be?
anyone else had this problem????
thanks
- 12-18-2009, 03:01 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I don't really understand the question.
Post the actual commands you are using (and indicate where the java source files are) and post the exact and entire output you get. The compiler's are very useful.
- 12-19-2009, 01:55 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
Here is the output which am am getting at the mement
as you can see one of the java files has compiled with no problemJava Code:paul@paul-laptop:~/Documents/OO/week6/part2$ ls BankAccount.class BankAccount.java~ BankTest1.java~ ScannerTest.java~ BankAccount.java BankTest1.java ScannerTest.java paul@paul-laptop:~/Documents/OO/week6/part2$ javac BankAccount.java paul@paul-laptop:~/Documents/OO/week6/part2$ javac BankTest1.java BankTest1.java:15: cannot find symbol symbol : class BankAccount location: class BankTest1 BankAccount[] customer = new BankAccount[3]; ^ BankTest1.java:15: cannot find symbol symbol : class BankAccount location: class BankTest1 BankAccount[] customer = new BankAccount[3]; ^ BankTest1.java:16: cannot find symbol symbol : class BankAccount location: class BankTest1 customer[0] = new BankAccount("A1","paul", 90.00); ^ BankTest1.java:17: cannot find symbol symbol : class BankAccount location: class BankTest1 customer[1] = new BankAccount("A2","John", 80.00); ^ BankTest1.java:18: cannot find symbol symbol : class BankAccount location: class BankTest1 customer[2] = new BankAccount("A3","Dave", 30.00); ^ 5 errors
- 12-19-2009, 02:05 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I'm assuming that neither class is part of a package. If not, then say. (*)
What happens if you explicitly set the classpath with
Java Code:paul@paul-laptop:~/Documents/OO/week6/part2$ javac -cp . BankTest1.java
or
Java Code:paul@paul-laptop:~/Documents/OO/week6/part2$ javac -cp . *.java
[Edit] * In fact, to confirm that there is nothing strange in the code, could you post the test class?Last edited by pbrockway2; 12-19-2009 at 02:08 AM.
- 12-19-2009, 03:06 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
thats the classJava Code:import java.util.*; public class BankTest1 { public static void main(String []args) { Scanner sc = new Scanner(System.in); //BankAccount customer; String AccountNo; String Name; double Money = 0; BankAccount[] customer = new BankAccount[3]; customer[0] = new BankAccount("A1","paul", 90.00); customer[1] = new BankAccount("A2","John", 80.00); customer[2] = new BankAccount("A3","Dave", 30.00); do { System.out.println("Enter Account Number"); AccountNo = sc.next(); for(int i = 0; i < 3; i++) { //boolean isAc = (customer[i].getAccountNumber() == AccountNo); if(customer[1].getAccountNumber() == AccountNo) { System.out.println("deposite amount"); Money = sc.nextDouble(); customer[i].deposit(Money); } } }while(!"0".equals(AccountNo)); //customer.deposit(1981); //customer.withdraw(1978); } }
The javac -cp . part did allow me to compile but when running the following happened
Java Code:paul@paul-laptop:~/Documents/OO/week6/part2$ javac -cp . BankTest1.java paul@paul-laptop:~/Documents/OO/week6/part2$ java BankTest1 Exception in thread "main" java.lang.NoClassDefFoundError: BankTest1 Caused by: java.lang.ClassNotFoundException: BankTest1 at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:319) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:264) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332) Could not find the main class: BankTest1. Program will exit.
- 12-19-2009, 03:49 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
OK.
The "classpath" is the list of places that the Java executables look to find classes. By "executables" I mean the compiler (javac), the runtime (java) and other things. The classpath can be set in various ways including a system variable CLASSPATH. In fact the most flexible way of setting the classpath is to specify it as part of the command using the -cp flag. This will override any CLASSPATH variable and make the classpath exactly what it needs to be.
"-cp ." means "make the classpath this directory I am in now". As a result the compiler will look for the classes it needs (like BankAccount) in the current directory. And all will be well as you've found.
The thing is to use the -cp flag with the java command:
Java Code:paul@paul-laptop:~/Documents/OO/week6/part2$ java -cp . BankTest1
(Sorry I've written so much to introduce such a tiny command but it's as well to be aware of what is going on.)
- 12-19-2009, 05:12 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 12
- Rep Power
- 0
Thanks for your help.
It worked perfectly
just one question. Why is it that the javac -cp . was needed for compiling in linux but javac was ok in windows?
- 12-19-2009, 08:48 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
It's not the operating system, but rather the configuration of the computer that is the problem. If you found that java worked on the Windows machine the reason was most likely that there was no CLASSPATH variable set (in which case the default value of "current directory" is used), but on the linux machine someone had set the CLASSPATH variable.
One good thing about "-cp ." is that it will have the desired effect whatever the configuration of the machine. The other thing is what I referred to before: it's flexible. As you write more (and more complex) programs possibly with multiple versions there may no longer be any sensible meaning to assign to *the* classpath. There's a different desired classpath for each task.
Make it a habit to type "-cp ." (or whatever you intend the classpath to be). It will work whatever the CLASSPATH setting is and it will force you to actually think about what the classpath should be: something that becomes important once you start using packages.
Similar Threads
-
I am confused
By prof.deedee in forum New To JavaReplies: 6Last Post: 10-30-2009, 11:32 PM -
Very confused Plz help!!
By ratb0y in forum NetBeansReplies: 0Last Post: 02-14-2009, 04:34 PM -
compiler,JIT compiler & interpreter
By gamilah in forum New To JavaReplies: 4Last Post: 11-04-2008, 12:32 AM -
a lot confused
By vineethraj in forum New To JavaReplies: 4Last Post: 01-18-2008, 12:36 AM -
what does it mean:confused:
By sivasayanth in forum New To JavaReplies: 2Last Post: 01-12-2008, 04:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks