#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    121
    Rep Power
    21

    [SOLVED] Basic java question on package not found error


    EDIT:
    Problem turned out to be with the CLASSPATH environment variable; If I want javac to search for packages in the current directory tree, do not create a CLASSPATH variable
    In my root folder (not drive root) , I have this file:
    Code:
    // filename: Converter.java
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
      import media.InvalidMediaFormatException;
      import media.Media;
    // import media.*;
     
    public class Converter
    {
       public static void main(String[] args)
       {
          if (args.length != 2)
          {
             System.err.println("usage: java Converter srcfile dstfile");
             return;
          }
          try
          {
             Media.convert(args[0], args[1]);
          }
          catch (InvalidMediaFormatException imfe)
          {
             System.out.println("Unable to convert " + args[0] + " to " + args[1]);
             System.out.println("Expecting " + args[0] + " to conform to " +
                                imfe.getExpectedFormat() + " format.");
             System.out.println("However, " + args[0] + " conformed to " +
                                imfe.getExistingFormat() + " format.");
          }
          catch (FileNotFoundException fnfe)
          {
          }
          catch (IOException ioe)
          {
          }
       }
    }
    In this folder I also have a folder called media, in which I have the following two files:

    Code:
    // filename: InvalidMediaFormatException .java
    package media;
     
    public class InvalidMediaFormatException extends Exception
    {
       private String expectedFormat;
       private String existingFormat;
       public InvalidMediaFormatException(String expectedFormat,
                                          String existingFormat)
       {
          super("Expected format: " + expectedFormat + ", Existing format: " +
                existingFormat);
          this.expectedFormat = expectedFormat;
          this.existingFormat = existingFormat;
       }
     
       public String getExpectedFormat()
       {
          return expectedFormat;
       }
     
       public String getExistingFormat()
       {
          return existingFormat;
       }
    }
    and

    Code:
    // Media.java
    package media;
     
    import java.io.IOException;
     
    public final class Media
    {
       public static void convert(String srcName, String dstName)
          throws InvalidMediaFormatException, IOException
       {
          if (srcName == null)
             throw new NullPointerException(srcName + " is null");
          if (dstName == null)
             throw new NullPointerException(dstName + " is null");
          // Code to access source file and verify that its format matches the
          // format implied by its file extension.
          //
          // Assume that the source file's extension is RM (for Real Media) and
          // that the file's internal signature suggests that its format is
          // Microsoft WAVE.
          String expectedFormat = "RM";
          String existingFormat = "WAVE";
          throw new InvalidMediaFormatException(expectedFormat, existingFormat);
       }
    }
    But at the root folder, if I do
    Code:
    javac Converter.java
    it keeps on giving me the error
    Code:
    package media does not exist
    . Why is this error still coming?
    Last edited by mahaju; July 25th, 2013 at 11:38 PM. Reason: [SOLVED]
  2. #2
  3. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,540
    Rep Power
    1621

    specifying classpath during compilation


    Refer to the materials or user guide provided by Oracle Java site, you should specify the classpath of your application by providing the switch -classpath when you issue the command javac against your application during compilation.

    There are actually many ways to compile an application. Depending on which way you compile your application, you should always specify the classpath whenever you issue javac command. In case you have set the CLASSPATH environment variable, or you are issuing the javac command at the classpath of your application itself, then it may not be necessary to specify the classpath explicitly. However, it is always the best practice to specify it using the -classpath switch of javac.
    When the programming world turns decent, the real world will turn upside down.
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    javac to search for packages in the current directory tree
    Be sure to include a . in the CLASSPATH variable to include the current diredtory.
    For example:
    .;d:\JAVADE~1\testing\JMF21~1.1E\lib\sound.jar;d:\JAVADE~1\testing\JMF21~1.1E\lib\jmf.jar;d:\JAVADE~ 1\testing\JMF21~1.1E\lib

IMN logo majestic logo threadwatch logo seochat tools logo