Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: .java File not found

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question .java File not found

    I am using javac in Windows command prompt to compile a basic hello world application that uses 2 source files (this is for a java certification exercise, I'm learning java). The source files are located in these directories

    "C:\Program Files\Java\jdk1.6.0_26\src\com\scjaexam\tutorial" is the location of GreetingsUniverse.java
    "C:\Program Files\Java\jdk1.6.0_26\src\com\scjaexam\tutorial\p lanets" is the location of Planets.java

    I was at the prompt:

    C:\Program Files\Java\jdk1.6.0_26>
    and typed
    javac -cp src\com\scjaexam\tutorial;src\com\scjaexam\tutoria l\planets GreetingsUniverse.java Planets.java
    and receive the following message:
    javac: file not found: GreetingsUniverse.java
    usage: Javac <options> <source files>
    use -help for a list of possible options
    So i checked to make sure the files are in the correct directories, and made sure the spelling is correct on the files, but am stumped why I am receiving this message?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: .java File not found

    The compiler is still trying to find your .java files in the current directory. What you need to do is cd to the directory of your main class and compile from there and only add the path to the other file using the -cp option.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    Ok, tried that and got these results:

    C:\Program Files\Java\jdk1.6.0_26\src\com\scjaexam\tutorial>j avac -cp .;planets GreetingsUniverse.java planets.java
    javac: file not found: planets.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: .java File not found

    javac -cp path/to/other/class ClassWithMainMethod.java
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    [Ok, I tried what you posted, and I'm receiving compiler errors due to the fact that the main class(GreetingsUniverse.java) instantiates the classes defined in Planets.java. but cannot find them.

    I don't know if this will help, but here is the code for both files:

    GreetingsUniverse.java:

    package com.scjaexam.tutorial;
    public class GreetingsUniverse {
    	public static void main(String[] args) {
    		System.out.println("Greetings, Universe!");
    		Earth e = new Earth();
    		Venus v = new Venus();
    		Mars m = new Mars();
    	}
    }

    and planets.java:

    package com.scjaexam.tutorial.planets;
    public class Earth {
    	public Earth {
    		System.out.println("Hello from Earth!");
    	}
    }
    public class Mars {
    	public Mars {
    		System.out.println("Hello from Mars!");
    	}
    }
    public class Venus {
    	public Venus {
    		System.out.println("Hello from Venus!");
    	}
    }

    and the compiler errors from entering the javac statement

    C:\Program Files\Java\jdk1.6.0_26\src\com\scjaexam\tutorial>j avac -cp planets GreetingsUniverse.java
    GreetingsUniverse.java:5: cannot find symbol
    symbol : class Earth
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Earth e = new Earth();
    ^
    GreetingsUniverse.java:5: cannot find symbol
    symbol : class Earth
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Earth e = new Earth();
    ^
    GreetingsUniverse.java:6: cannot find symbol
    symbol : class Venus
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Venus v = new Venus();
    ^
    GreetingsUniverse.java:6: cannot find symbol
    symbol : class Venus
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Venus v = new Venus();
    ^
    GreetingsUniverse.java:7: cannot find symbol
    symbol : class Mars
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Mars m = new Mars();
    ^
    GreetingsUniverse.java:7: cannot find symbol
    symbol : class Mars
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Mars m = new Mars();
    ^
    6 errors

    C:\Program Files\Java\jdk1.6.0_26\src\com\scjaexam\tutorial>

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: .java File not found

    Look at the package statements. Your both classes are in different packages.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    Quote Originally Posted by Mr.777 View Post
    Look at the package statements. Your both classes are in different packages.
    ok, what you're saying makes sense to me, but the exercise tells me explicitly to put the packages as they are. I assume it's trying to teach me to use a certain feature of java or something, Here's the exercise and what it tells me to do, with package instructions bolded:

    When you compile and run packaged software from an IDE, it can be as easy as
    clicking a run icon as IDE’s support, setting the necessary paths that will be used
    by the compiler and interpreters. However, when you try to compile and interpret
    the code yourself from the command line, you will need to know exactly how
    to path your files. Consider our sample application that is now placed in the
    com.scjaexam.tutorial package.
    package com.scjaexam.tutorial;
    public class GreetingsUniverse {
    public static void main(String[] args) {
    System.out.println("Greetings, Universe!");
    }
    }
    This exercise will have you compiling and running the application with new
    classes created in a separate package.
    1. Compile the program.
    javac -d . GreetingsUniverse.java
    2. Run the program to ensure it is error-free.
    java -cp . com.scjaexam.tutorial.GreetingsUniverse.
    3. Create three classes named Earth, Mars, and Venus and place them in
    the com.scja.exam.tutorial.planets package. Create constructors
    that will print the names of the planets to standard out. Note that the details
    for the Earth class are given here as an example of what you will need to do.
    package com.scja.exam.tutorial.planets;
    public class Earth {
    public Earth {
    System.out.println("Hello from Earth!");
    }
    }
    4. Instantiate each class from the main program, by adding the necessary code
    to the GreetingsUniverse class.
    Earth e = new Earth();
    5. Ensure that all of the source code is in the paths src/com/scjaexam/tutorial/ and
    src/com/scjaexam/tutorial/planets/, respectively.

    6. Determine the command-line arguments needed to compile the complete
    program. Compile the program, and debug where necessary.

    7. Determine the command-line arguments needed to interpret the program.
    Run the program.
    The standard output will read:
    $ Greetings, Universe!
    Hello from Earth!
    Hello from Mars!
    Hello from Venus!

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: .java File not found

    You need to import the files, and for command line calling you need to NOT be in the directory of the file, but the directory of the beggining package.

    EG for importing, if you have two classes like
    package com.src.gui;
    and one in
    package com.src.driver.helper;
    Note you should look up common syntactically package naming and size of a package etc, more packages does not always mean better, and a helper class is only [b]rarely[/b] needed.  This is for example purposes only.
    You would import like so.
    package com.src.gui;
     
    import com.src.driver.helper.*;
     
    public class abcd
    {
     
    }
    and for the other one

    package com.src.driver.helper;
     
    import com.src.gui.*;
    public class hjkl
    {
      public static void main(String[] args)
      {
     
      }
    }

    and to call the hjkl class from console, lets say this was saved in a Java/Projects/SomeProject folder, so it would look something like
    C:\Users\Ben\Documents\Java\Projects\Project\SomeProject
    You would first cd into that directory, and call it like.

    javac com\src\driver\helper\hjkl.java
    java com.src.driver.helper.hjkl

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    ok, i've added an import statement to the helper class Planets.java, but I am now back to the file not found problem

    C:\Program Files\Java\jdk1.6.0_26\src>javac -cp com\scjaexam\tutorial;com\scjaex
    am\tutorial\planets GreetingsUniverse.java planets.java
    javac: file not found: GreetingsUniverse.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: .java File not found

    First, do NOT place your source files under the JDK/JRE directories. Imagine updating those applications and POOF, source all gone.

    Second, your Planets.java contains no Planets class. See the java language spec ( Packages ) - in more simple words, strip out all the classes into their own separate .java files.

    Lastly, if you are using packages, cd into the package parent directory, and compile from there - pointing the compiler to the class file you wish to compile. The package structure should tell the compiler where to look for the files - there should not be a need to add them to the classpath unless they are located outside the package structure. For example, something like: 'javac com/scjaexam/tutorial/GrettingsUniverse.java'

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    Quote Originally Posted by copeg View Post
    First, do NOT place your source files under the JDK/JRE directories. Imagine updating those applications and POOF, source all gone.

    Second, your Planets.java contains no Planets class. See the java language spec ( Packages ) - in more simple words, strip out all the classes into their own separate .java files.

    Lastly, if you are using packages, cd into the package parent directory, and compile from there - pointing the compiler to the class file you wish to compile. The package structure should tell the compiler where to look for the files - there should not be a need to add them to the classpath unless they are located outside the package structure. For example, something like: 'javac com/scjaexam/tutorial/GrettingsUniverse.java'
    ok i got these new files. and I moved them from jdk to the parent directory java:

    And I think i understand what you're saying about classpath, but when you give it the directory to search, does it search subfolders too?

    Earth.java

    package com.scjaexam.tutorial.planets;
    public class Earth {
    	public Earth {
    		System.out.println("Hello from Earth!");
    	}
    }

    Venus.java

    package com.scjaexam.tutorial.planets;
     
    public class Venus {
    	public Venus {
    		System.out.println("Hello from Venus!");
    	}
    }

    and Mars.Java

    package com.scjaexam.tutorial.planets;
    public class Mars {
    	public Mars {
    		System.out.println("Hello from Mars!");
    	}
    }

    and added import statements to the GreetingsUniverse.java file

    package com.scjaexam.tutorial;
    import com.scjaexam.tutorial.planets.Venus;
    import com.scjaexam.tutorial.planets.Earth;
    import com.scjaexam.tutorial.planets.Mars;
    public class GreetingsUniverse {
    	public static void main(String[] args) {
    		System.out.println("Greetings, Universe!");
    		Earth e = new Earth();
    		Venus v = new Venus();
    		Mars m = new Mars();
    	}
    }

    But now, when I run it, it says that the com.scjaexam.tutorial.planets package doesn't exist. Here look:

    C:\Program Files\Java\src>javac -cp . com\scjaexam\tutorial\GreetingsUniverse.ja
    va
    com\scjaexam\tutorial\GreetingsUniverse.java:2: package com.scjaexam.tutorial.pl
    anets does not exist
    import com.scjaexam.tutorial.planets.Venus;
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:3: package com.scjaexam.tutorial.pl
    anets does not exist
    import com.scjaexam.tutorial.planets.Earth;
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:4: package com.scjaexam.tutorial.pl
    anets does not exist
    import com.scjaexam.tutorial.planets.Mars;
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:8: cannot find symbol
    symbol : class Earth
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Earth e = new Earth();
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:8: cannot find symbol
    symbol : class Earth
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Earth e = new Earth();
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:9: cannot find symbol
    symbol : class Venus
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Venus v = new Venus();
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:9: cannot find symbol
    symbol : class Venus
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Venus v = new Venus();
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:10: cannot find symbol
    symbol : class Mars
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Mars m = new Mars();
    ^
    com\scjaexam\tutorial\GreetingsUniverse.java:10: cannot find symbol
    symbol : class Mars
    location: class com.scjaexam.tutorial.GreetingsUniverse
    Mars m = new Mars();
    ^
    9 errors

    C:\Program Files\Java\src>
    this seems weird to me, Almost as if I need to import the .planets package before importing each planet class?

    thanks in advance.

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: .java File not found

    Is your directory structure organizes as the package is organized? In other words, do you have a planets directory in the same directory that the GreetingsUniverse.java is located? Are the java files for Venus, Mars, etc...within that directory (the directory com/scjaexam/tutorial/plantes/), and your GreetingsUniverse.java inside the com/scjaexam/tutorial directory?

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    Quote Originally Posted by copeg View Post
    Is your directory structure organizes as the package is organized? In other words, do you have a planets directory in the same directory that the GreetingsUniverse.java is located? Are the java files for Venus, Mars, etc...within that directory (the directory com/scjaexam/tutorial/plantes/), and your GreetingsUniverse.java inside the com/scjaexam/tutorial directory?
    Yes exactly, that is what has been so confusing to me. Are the package names case sensitive with the directory names?

  14. #14
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    Wow... I changed the "Planets" directory to "planets" in the windows folder, and got a whole different bunch of errors



    C:\Program Files\Java\src>javac -cp . com\scjaexam\tutorial\GreetingsUniverse.ja
    va
    .\com\scjaexam\tutorial\planets\Venus.java:4: <identifier> expected
    public Venus {
    ^
    .\com\scjaexam\tutorial\planets\Venus.java:5: <identifier> expected
    System.out.println("Hello from Venus!");
    ^
    .\com\scjaexam\tutorial\planets\Venus.java:5: illegal start of type
    System.out.println("Hello from Venus!");
    ^
    .\com\scjaexam\tutorial\planets\Venus.java:7: class, interface, or enum expected

    }
    ^
    .\com\scjaexam\tutorial\planets\Earth.java:3: <identifier> expected
    public Earth {
    ^
    .\com\scjaexam\tutorial\planets\Earth.java:4: <identifier> expected
    System.out.println("Hello from Earth!");
    ^
    .\com\scjaexam\tutorial\planets\Earth.java:4: illegal start of type
    System.out.println("Hello from Earth!");
    ^
    .\com\scjaexam\tutorial\planets\Earth.java:6: class, interface, or enum expected

    }
    ^
    .\com\scjaexam\tutorial\planets\Mars.java:3: <identifier> expected
    public Mars {
    ^
    .\com\scjaexam\tutorial\planets\Mars.java:4: <identifier> expected
    System.out.println("Hello from Mars!");
    ^
    .\com\scjaexam\tutorial\planets\Mars.java:4: illegal start of type
    System.out.println("Hello from Mars!");
    ^
    .\com\scjaexam\tutorial\planets\Mars.java:6: class, interface, or enum expected
    }
    ^
    .\com\scjaexam\tutorial\planets\Venus.java:5: cannot find symbol
    symbol : class out
    location: class java.lang.System
    System.out.println("Hello from Venus!");
    ^
    .\com\scjaexam\tutorial\planets\Earth.java:4: cannot find symbol
    symbol : class out
    location: class java.lang.System
    System.out.println("Hello from Earth!");
    ^
    .\com\scjaexam\tutorial\planets\Mars.java:4: cannot find symbol
    symbol : class out
    location: class java.lang.System
    System.out.println("Hello from Mars!");
    ^
    15 errors

    C:\Program Files\Java\src>

  15. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: .java File not found

    Because Java is case sensitive language. Anything related to java is case sensitive.

  16. #16
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    ok, but I'm confused about the first error, "identifier expected", That line is a constructor, I thought for constructors you put the (private/public) in and don't give identifiers, just give the name of the class and the compiler knows that it's a constructor?

  17. #17
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: .java File not found

    You have to include the () because you can still specify parameters. I made an example class shown below.

    import java.util.ArrayList;
    /**
     * This is an example class to show how constructors work.
     * 
     * A sandwich has many properties, which you may not have to specify.  
     * You could just have a name, ham sandwhich, and assume that the user
     * knows what that contains, or you could just have a list of ingrediants,
     * or maybe you could have both.
     * @author TJStretch
     */
    public class Fruit
    {
      /**
       * The name of this fruit, to get use getName, to set use setName
       */
      private String name;
      /**
       * Ingredients of this fruit, has getter and setter methods.
       */
      private ArrayList<String> ingredients;
      /**
       * Creates a default Fruit that can be modified via the JBeans method.
       * Before the name or ingredients are set, calling toString on this instance
       * will throw a NullPointerException.
       */
      public Fruit()
      {
        /*Implementation not shown*/
      }
      /**
       * This creates a Fruit object with the specified name and empty ingredients.
       * Ingredients can be modified later using setIngrediants or addIngrediants
       * without any errors being thrown
       * @param name the name
       */
      public Fruit(String name)
      {
        /*Implementation not shown*/
      }
      /**
       * Create a Fruit object with only the specified ingredients.  You can set the name
       * later using the setName method.  This can be more convienient than creating a String[] array
       * of your ingredients for the ingredients as you would have to in the other constructor.
       * @param ingredients
       */
      public Fruit(String... ingredients)
      {
        /*Implementation not shown*/
      }
      /**
       * Creates a Fruit with only the specified ingredients. Name can be modified
       * later using the setName method.
       * @param ingredients
       */
      public Fruit(String name, String[] ingredients)
      {
        /*Implementation not shown*/
      }
     
      public static void main(String[] args)
      {
        Fruit hamSandwich = new Fruit("Ham Sandwich"); //Create a fruit named ham sandwich
        Fruit turkeySand = new Fruit("Turkey", "Mannas", "Salt", "Bread"); //Createa turkey sandwich with only ingredients
        Fruit hamAndTurkey = new Fruit("Ham and Turkey Sandwich", new String[]{"Ham", "Turkey", "Mannas", "Salt", "Bread"});
        System.out.println(hamSandwich);
        System.out.println(turkeySand);
        System.out.println(hamAndTurkey);
      }
      /*
       * Precondition: Fruit has a name that has been initialized, and ingredients
       * is a ArrayList<String> that does not have to have been initialized.
       */
      @Override
      public String toString()
      {
        if(name == null)
          throw new NullPointerException("The setName() method or a constructor with name specified is necessary!");
        if(ingredients == null || ingredients.size() == 0)
          return "["+name+"]";
        String result = "{ "+name+"["+ingredients.get(0);
        for(int i = 1; i < ingredients.size(); i++)
        {
          result += ", "+ingredients.get(i);
        }
        result += "] }";
        return result;
      }
    }

  18. #18
    Junior Member
    Join Date
    Nov 2011
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: .java File not found

    oh ok, this is making sense to me now. I just didn't recognize the error in the command prompt, I've had this error before in other languages. I applied the changes and the code compiled without any problems! thanks much!

  19. #19
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: .java File not found

    Quote Originally Posted by MeteoricDragon View Post
    ok, but I'm confused about the first error, "identifier expected", That line is a constructor, I thought for constructors you put the (private/public) in and don't give identifiers, just give the name of the class and the compiler knows that it's a constructor?
    Remember, constructor is a special type of function that returns nothing (Not even void). And in any function definition, opening and closing braces are part of it (). In your above code, there is no () in any of specified constructor. So, that is why, compiler is prompting for error. As it expected an identifier there. So, by simply placing the () in front of your constructor's name, will definitely make it understandble for compiler.

Similar Threads

  1. java.lang.IllegalArgumentException: Identifier not found
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 9th, 2012, 04:48 AM
  2. java.sql.SQLException: No data found
    By Virender in forum JDBC & Databases
    Replies: 2
    Last Post: December 7th, 2011, 01:17 PM
  3. File not found exception
    By mwr76 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: September 10th, 2011, 03:54 PM
  4. application id not found
    By pradeepsetty in forum Java IDEs
    Replies: 3
    Last Post: June 2nd, 2010, 02:37 AM
  5. Replies: 2
    Last Post: January 8th, 2010, 08:22 AM