Results 1 to 8 of 8
  1. #1
    paul21 is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

  3. #3
    paul21 is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    Here is the output which am am getting at the mement
    Java 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
    as you can see one of the java files has compiled with no problem

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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 03:08 AM.

  5. #5
    paul21 is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    Java 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);
    
    	
    	}
    }
    thats the class


    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.)

  7. #7
    paul21 is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    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?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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

  1. I am confused
    By prof.deedee in forum New To Java
    Replies: 6
    Last Post: 10-31-2009, 12:32 AM
  2. Very confused Plz help!!
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-14-2009, 05:34 PM
  3. compiler,JIT compiler & interpreter
    By gamilah in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 01:32 AM
  4. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 01:36 AM
  5. what does it mean:confused:
    By sivasayanth in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 05:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •