compile error - basic package issue

Discussion in 'Java' started by tom, Jan 15, 2010.

  1. tom

    tom Guest

    Hi.

    New to java, and trying to create a test app that imports another test
    package.

    The test apps are listed, as is the compile cmd and the err..

    The files are all in the same dir. I was able to compile the
    MyIncorrectnessListener.java, and the MycssErrorHandler.java with no
    warnings/errs.

    Any pointers/comments are appreciated...

    thanks

    the compile cmd/err
    [root@toshiba parseapp2]# javac -Xlint -classpath '/opt/htmlunit/lib/
    *:/parseapp2/' sjsu_classes.java
    sjsu_classes.java:33: '.' expected
    import MyIncorrectnessListener;
    ^
    sjsu_classes.java:33: ';' expected
    import MyIncorrectnessListener;
    ^
    sjsu_classes.java:34: class, interface, or enum expected
    import MycssErrorHandler;
    ^
    3 errors
    3 warnings
    [


    /*
    // sjsu_classes.java
    //
    // function handles getting the data from the sjsu course page, and
    returns the course page/list
    // for the given Alphabet letter. - handles ane letter at a time..
    // -the page is then returned to the calling routinne
    //
    // input:
    // args[0] - start url
    // args[1] - term
    // args[2] - dept
    //
    // return:
    // course page for the term/dept
    //
    */
    import java.io.IOException;
    import java.net.URL;
    import java.util.List;
    import java.util.Set;

    import org.apache.commons.httpclient.NameValuePair;

    import com.gargoylesoftware.htmlunit.*;
    import com.gargoylesoftware.htmlunit.html.*;
    import org.w3c.css.sac.ErrorHandler;
    import org.xml.sax.SAXParseException;

    //==my test packages
    import MyIncorrectnessListener;
    import MycssErrorHandler.*;

    public class sjsu_classes {

    //public static void main(String[] args) throws Exception {
    public void main(String[] args) throws Exception {

    // Create and initialize WebClient object
    WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);

    IncorrectnessListener ilisten = new MyIncorrectnessListener();
    ErrorHandler ierr = new MycssErrorHandler();
    webClient.setIncorrectnessListener(ilisten);
    webClient.setCssErrorHandler(ierr);
    webClient.setThrowExceptionOnScriptError(false);
    webClient.setRefreshHandler(new RefreshHandler() {
    public void handleRefresh(Page page, URL url, int arg) throws
    IOException {
    //System.out.println("handleRefresh");
    }

    });

    // visit the page, get the form, get the btn
    String url_tmp;
    String url;
    String Term;
    String Dept;
    String tmp1;
    String tmp2;

    url_tmp="?
    FolderPath=PORTAL_ROOT_OBJECT.PA_HC_CLASS_SEARCH&PortalActualURL=https
    %3a%2f%2fcmshr.sjsu.edu%2fpsc%2fHSJPRDF%2fEMPLOYEE%2fHSJPRD%2fc
    %2fCOMMUNITY_ACCESS.CLASS_SEARCH.GBL&PortalRegistryName=EMPLOYEE&PortalServletURI=https
    %3a%2f%2fcmshr.sjsu.edu%2fpsp%2fHSJPRDF%2f&PortalURI=https%3a%2f
    %2fcmshr.sjsu.edu%2fpsc%2fHSJPRDF
    %2f&PortalHostNode=HRMS&NoCrumbs=yes";

    System.exit(0);
    }
    }


    MyIncorrectnessListener test package
    --------------------------------------------
    /*
    // MyIncorrectnessListener.java
    //
    // function handles getting the data from the sjsu course page, and
    returns the course page/list
    // for the given Alphabet letter. - handles ane letter at a time..
    // -the page is then returned to the calling routinne
    //
    // input:
    // args[0] - start url
    // args[1] - term
    // args[2] - dept
    //
    // return:
    // course page for the term/dept
    //
    */
    //==handle the warnings thrown from the js functions..

    package MyIncorrectnessListener;

    import com.gargoylesoftware.htmlunit.*;
    import com.gargoylesoftware.htmlunit.html.*;

    //public static class MyIncorrectnessListener implements
    IncorrectnessListener
    public class MyIncorrectnessListener implements IncorrectnessListener
    {
    @Override
    public void notify(String arg0, Object arg1)
    {
    //System.err.println("Argument : " + arg0.toString() + ", Object :
    ");
    }
    }


    MycssErrorHandler test package
    --------------------------------
    /*
    // MycssErrorHandler.java
    //
    // function handles getting the data from the sjsu course page, and
    returns the course page/list
    // for the given Alphabet letter. - handles ane letter at a time..
    // -the page is then returned to the calling routinne
    //
    // input:
    // args[0] - start url
    // args[1] - term
    // args[2] - dept
    //
    // return:
    // course page for the term/dept
    //
    */
    package MycssErrorHandler;

    import org.w3c.css.sac.ErrorHandler;
    import org.xml.sax.SAXParseException;
    import com.gargoylesoftware.htmlunit.*;
    import com.gargoylesoftware.htmlunit.html.*;

    //==handle the warnings thrown from the js functions..
    //==handle the warnings thrown from the css functions..
    // public static class MycssErrorHandler implements
    DefaultCssErrorHandler
    // public static class MycssErrorHandler implements ErrorHandler
    // public class MycssErrorHandler implements ErrorHandler
    public abstract class MycssErrorHandler implements ErrorHandler
    // protected class MycssErrorHandler implements ErrorHandler
    {
    //@Override
    public void notify(String arg0, Object arg1)
    {
    //System.err.println("Argument : " + arg0.toString() + ",
    Object : ");
    }
    //@Override
    public void fatalError(SAXParseException ex)
    {
    //fatals.add(ex);
    }


    }
    tom, Jan 15, 2010
    #1
    1. Advertising

  2. tom

    tom Guest

    On Jan 15, 2:43 pm, Peter Duniho <> wrote:
    > tom wrote:
    > > Hi.

    >
    > > New to java, and trying to create a test app that imports another test
    > > package.

    >
    > You don't import packages.  You import types (classes).
    >
    > You can import every type in a package by including a wildcard.  For
    > example:
    >
    >    import MyIncorrectnessListener.*;
    >
    > See your other source files that compile correctly for examples of both
    > importing specific types and importing all types in a package.
    >
    > For what it's worth, it seems to me that the error emitted told you
    > fairly directly what was wrong.  It specifically explains that a period
    > ('.') was expected at the point where you'd put a semi-colon (';').
    >
    > So your first step in diagnosing the problem could (and IMHO should)
    > have been to consider the question as to why a period might have been
    > expected there (i.e. because a package name by itself is insufficient),
    > and what might follow a period once you put it there (i.e. a specific
    > type in the given package, or a wildcard indicating all types in the
    > given package).
    >
    > Pete


    already had tried that prior to posting here...

    gave an error, which led to the thought that something else must be
    going on, which was why this was posted here...

    but thanks..

    tom
    tom, Jan 15, 2010
    #2
    1. Advertising

  3. tom

    tom Guest

    On Jan 15, 3:33 pm, tom <> wrote:
    > On Jan 15, 2:43 pm, Peter Duniho <> wrote:
    >
    >
    >
    > > tom wrote:
    > > > Hi.

    >
    > > > New to java, and trying to create a test app that imports another test
    > > > package.

    >
    > > You don't import packages.  You import types (classes).

    >
    > > You can import every type in a package by including a wildcard.  For
    > > example:

    >
    > >    import MyIncorrectnessListener.*;

    >
    > > See your other source files that compile correctly for examples of both
    > > importing specific types and importing all types in a package.

    >
    > > For what it's worth, it seems to me that the error emitted told you
    > > fairly directly what was wrong.  It specifically explains that a period
    > > ('.') was expected at the point where you'd put a semi-colon (';').

    >
    > > So your first step in diagnosing the problem could (and IMHO should)
    > > have been to consider the question as to why a period might have been
    > > expected there (i.e. because a package name by itself is insufficient),
    > > and what might follow a period once you put it there (i.e. a specific
    > > type in the given package, or a wildcard indicating all types in the
    > > given package).

    >
    > > Pete

    >
    > already had tried that prior to posting here...
    >
    > gave an error, which led to the thought that something else must be
    > going on, which was why this was posted here...
    >
    > but thanks..
    >
    > tom



    also..

    when tried to have :
    import MyIncorrectnessListener.*;
    import MycssErrorHandler.*;

    got the compile err:
    sjsu_classes.java:30: package MyIncorrectnessListener does not exist
    import MyIncorrectnessListener.*;
    ^
    sjsu_classes.java:31: package MycssErrorHandler does not exist
    import MycssErrorHandler.*;
    ^

    bad class file: /parseapp2/MyIncorrectnessListener.class
    class file contains wrong class:
    MyIncorrectnessListener.MyIncorrectnessListener
    Please remove or make sure it appears in the correct subdirectory of
    the classpath.
    IncorrectnessListener ilisten = new MyIncorrectnessListener();


    thoughts/pointers...

    thanks



    which is strange, given that there is clearly the
    MyIncorrectnessListener.java and the MyIncorrectnessListener.class
    files in the same dir as the parent java file...
    tom, Jan 15, 2010
    #3
  4. tom

    Roedy Green Guest

    On Fri, 15 Jan 2010 14:12:47 -0800 (PST), tom <>
    wrote, quoted or indirectly quoted someone who said :

    >Any pointers/comments are appreciated...


    see http://mindprod.com/jgloss/package.html
    http://mindprod.com/jgloss/import.html
    http://mindprod.com/jgloss/classpath.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I decry the current tendency to seek patents on algorithms. There are better ways to earn a living than to prevent other people from making use of one’s contributions to computer science.
    ~ Donald Ervin Knuth (born: 1938-01-10 age: 72)
    Roedy Green, Jan 16, 2010
    #4
  5. tom

    Roedy Green Guest

    On Fri, 15 Jan 2010 15:45:40 -0800 (PST), tom <>
    wrote, quoted or indirectly quoted someone who said :

    > import MyIncorrectnessListener.*;


    by convention package names are all lower case and class names start
    with a capital letter. If you follow that convention, your code will
    be much less confusing as to your intentions, even when it is wrong,
    both to yourself and others.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I decry the current tendency to seek patents on algorithms. There are better ways to earn a living than to prevent other people from making use of one’s contributions to computer science.
    ~ Donald Ervin Knuth (born: 1938-01-10 age: 72)
    Roedy Green, Jan 16, 2010
    #5
  6. tom

    Silvio Guest

    "tom" <> wrote in message
    news:...
    > Hi.
    >
    > New to java, and trying to create a test app that imports another test
    > package.
    >
    > The test apps are listed, as is the compile cmd and the err..
    >
    > The files are all in the same dir. I was able to compile the
    > MyIncorrectnessListener.java, and the MycssErrorHandler.java with no
    > warnings/errs.
    >
    > Any pointers/comments are appreciated...
    >
    > thanks
    >
    > the compile cmd/err
    > [root@toshiba parseapp2]# javac -Xlint -classpath '/opt/htmlunit/lib/
    > *:/parseapp2/' sjsu_classes.java
    > sjsu_classes.java:33: '.' expected
    > import MyIncorrectnessListener;
    > ^
    > sjsu_classes.java:33: ';' expected
    > import MyIncorrectnessListener;
    > ^
    > sjsu_classes.java:34: class, interface, or enum expected
    > import MycssErrorHandler;
    > ^
    > 3 errors
    > 3 warnings
    > [
    >
    >
    > /*
    > // sjsu_classes.java
    > //
    > // function handles getting the data from the sjsu course page, and
    > returns the course page/list
    > // for the given Alphabet letter. - handles ane letter at a time..
    > // -the page is then returned to the calling routinne
    > //
    > // input:
    > // args[0] - start url
    > // args[1] - term
    > // args[2] - dept
    > //
    > // return:
    > // course page for the term/dept
    > //
    > */
    > import java.io.IOException;
    > import java.net.URL;
    > import java.util.List;
    > import java.util.Set;
    >
    > import org.apache.commons.httpclient.NameValuePair;
    >
    > import com.gargoylesoftware.htmlunit.*;
    > import com.gargoylesoftware.htmlunit.html.*;
    > import org.w3c.css.sac.ErrorHandler;
    > import org.xml.sax.SAXParseException;
    >
    > //==my test packages
    > import MyIncorrectnessListener;
    > import MycssErrorHandler.*;
    >
    > public class sjsu_classes {
    >
    > //public static void main(String[] args) throws Exception {
    > public void main(String[] args) throws Exception {
    >
    > // Create and initialize WebClient object
    > WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
    >
    > IncorrectnessListener ilisten = new MyIncorrectnessListener();
    > ErrorHandler ierr = new MycssErrorHandler();
    > webClient.setIncorrectnessListener(ilisten);
    > webClient.setCssErrorHandler(ierr);
    > webClient.setThrowExceptionOnScriptError(false);
    > webClient.setRefreshHandler(new RefreshHandler() {
    > public void handleRefresh(Page page, URL url, int arg) throws
    > IOException {
    > //System.out.println("handleRefresh");
    > }
    >
    > });
    >
    > // visit the page, get the form, get the btn
    > String url_tmp;
    > String url;
    > String Term;
    > String Dept;
    > String tmp1;
    > String tmp2;
    >
    > url_tmp="?
    > FolderPath=PORTAL_ROOT_OBJECT.PA_HC_CLASS_SEARCH&amp;PortalActualURL=https
    > %3a%2f%2fcmshr.sjsu.edu%2fpsc%2fHSJPRDF%2fEMPLOYEE%2fHSJPRD%2fc
    > %2fCOMMUNITY_ACCESS.CLASS_SEARCH.GBL&amp;PortalRegistryName=EMPLOYEE&amp;PortalServletURI=https
    > %3a%2f%2fcmshr.sjsu.edu%2fpsp%2fHSJPRDF%2f&amp;PortalURI=https%3a%2f
    > %2fcmshr.sjsu.edu%2fpsc%2fHSJPRDF
    > %2f&amp;PortalHostNode=HRMS&amp;NoCrumbs=yes";
    >
    > System.exit(0);
    > }
    > }
    >
    >
    > MyIncorrectnessListener test package
    > --------------------------------------------
    > /*
    > // MyIncorrectnessListener.java
    > //
    > // function handles getting the data from the sjsu course page, and
    > returns the course page/list
    > // for the given Alphabet letter. - handles ane letter at a time..
    > // -the page is then returned to the calling routinne
    > //
    > // input:
    > // args[0] - start url
    > // args[1] - term
    > // args[2] - dept
    > //
    > // return:
    > // course page for the term/dept
    > //
    > */
    > //==handle the warnings thrown from the js functions..
    >
    > package MyIncorrectnessListener;
    >
    > import com.gargoylesoftware.htmlunit.*;
    > import com.gargoylesoftware.htmlunit.html.*;
    >
    > //public static class MyIncorrectnessListener implements
    > IncorrectnessListener
    > public class MyIncorrectnessListener implements IncorrectnessListener
    > {
    > @Override
    > public void notify(String arg0, Object arg1)
    > {
    > //System.err.println("Argument : " + arg0.toString() + ", Object :
    > ");
    > }
    > }
    >
    >
    > MycssErrorHandler test package
    > --------------------------------
    > /*
    > // MycssErrorHandler.java
    > //
    > // function handles getting the data from the sjsu course page, and
    > returns the course page/list
    > // for the given Alphabet letter. - handles ane letter at a time..
    > // -the page is then returned to the calling routinne
    > //
    > // input:
    > // args[0] - start url
    > // args[1] - term
    > // args[2] - dept
    > //
    > // return:
    > // course page for the term/dept
    > //
    > */
    > package MycssErrorHandler;
    >
    > import org.w3c.css.sac.ErrorHandler;
    > import org.xml.sax.SAXParseException;
    > import com.gargoylesoftware.htmlunit.*;
    > import com.gargoylesoftware.htmlunit.html.*;
    >
    > //==handle the warnings thrown from the js functions..
    > //==handle the warnings thrown from the css functions..
    > // public static class MycssErrorHandler implements
    > DefaultCssErrorHandler
    > // public static class MycssErrorHandler implements ErrorHandler
    > // public class MycssErrorHandler implements ErrorHandler
    > public abstract class MycssErrorHandler implements ErrorHandler
    > // protected class MycssErrorHandler implements ErrorHandler
    > {
    > //@Override
    > public void notify(String arg0, Object arg1)
    > {
    > //System.err.println("Argument : " + arg0.toString() + ",
    > Object : ");
    > }
    > //@Override
    > public void fatalError(SAXParseException ex)
    > {
    > //fatals.add(ex);
    > }
    >
    >
    > }



    You should ALWAYS put your classes in a package. That means that each Java
    source must start with a line like

    package my.package.name.domain.com;

    Additionally, you must then put the Java source in a directory
    my/package/name/domain/com/ relative to the root directory of all the Java
    sources in your project (most IDEs will allow you to have multiple root
    directories in a project but let's not complicate things here).

    This way your source directory content will mirror your package hierarchy.
    Java classes that are in the same package/directory do not need to be
    imported to refer to each other but when they are in different
    packages/directories then they can only "see" each other either by fully
    qualified name, like my.package.name.domain.com.SomeClass or by using
    imports.

    A final remark: you must run javac from the root of your source tree.

    Gr. Silvio
    Silvio, Jan 16, 2010
    #6
  7. tom

    Tom Anderson Guest

    On Sat, 16 Jan 2010, Silvio wrote:

    > A final remark: you must run javac from the root of your source tree.


    Not so. You can run it from anywhere you like, and pass in paths to source
    files rooted anywhere you like (ie the paths don't have to replicate the
    package names). The only constraint is that if the source files depend on
    classes which are not being named explicitly, then either their class
    files must be on an explicitly-given classpath, or their source files must
    be on an explicitly-given source path.

    tom

    --
    Postmodernism's refusal of master narratives is a neat dodge, but doesn't
    get it out of the problem that it offers a master narrative. A teapot
    with 'this is not a teapot' stencilled on the side will still hold your
    PG Tips. -- G'
    Tom Anderson, Jan 16, 2010
    #7
  8. tom

    Roedy Green Guest

    On Sat, 16 Jan 2010 12:06:07 +0100, "Silvio" <>
    wrote, quoted or indirectly quoted someone who said :

    >package my.package.name.domain.com;


    It is better to do it like this

    com.mindprod.somepackage.SomeClass;

    this way they sort properly.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    I decry the current tendency to seek patents on algorithms. There are better ways to earn a living than to prevent other people from making use of one’s contributions to computer science.
    ~ Donald Ervin Knuth (born: 1938-01-10 age: 72)
    Roedy Green, Jan 17, 2010
    #8
  9. tom

    Silvio Guest

    "Tom Anderson" <> wrote in message
    news:...
    > On Sat, 16 Jan 2010, Silvio wrote:
    >
    >> A final remark: you must run javac from the root of your source tree.

    >
    > Not so. You can run it from anywhere you like, and pass in paths to source
    > files rooted anywhere you like (ie the paths don't have to replicate the
    > package names). The only constraint is that if the source files depend on
    > classes which are not being named explicitly, then either their class
    > files must be on an explicitly-given classpath, or their source files must
    > be on an explicitly-given source path.
    >
    > tom
    >
    > --
    > Postmodernism's refusal of master narratives is a neat dodge, but doesn't
    > get it out of the problem that it offers a master narrative. A teapot
    > with 'this is not a teapot' stencilled on the side will still hold your
    > PG Tips. -- G'


    Yes, I know but for the OP who is obviously a beginner I was trying to keep
    it simple and point him towards the simplest use case. He should use an IDE
    anyway.

    Silvio
    Silvio, Jan 18, 2010
    #9
  10. tom

    Silvio Guest

    "Roedy Green" <> wrote in message
    news:...
    > On Sat, 16 Jan 2010 12:06:07 +0100, "Silvio" <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >>package my.package.name.domain.com;

    >
    > It is better to do it like this
    >
    > com.mindprod.somepackage.SomeClass;
    >
    > this way they sort properly.
    > --
    > Roedy Green Canadian Mind Products
    > http://mindprod.com
    > I decry the current tendency to seek patents on algorithms. There are
    > better ways to earn a living than to prevent other people from making use
    > of one's contributions to computer science.
    > ~ Donald Ervin Knuth (born: 1938-01-10 age: 72)


    Yep, typed that straight from the head without thinking too much. Completely
    reversed that and may have given the OP a bad example...
    Silvio, Jan 18, 2010
    #10
  11. tom

    Tom Anderson Guest

    On Mon, 18 Jan 2010, Silvio wrote:

    > "Tom Anderson" <> wrote in message
    > news:...
    >> On Sat, 16 Jan 2010, Silvio wrote:
    >>
    >>> A final remark: you must run javac from the root of your source tree.

    >>
    >> Not so. You can run it from anywhere you like, and pass in paths to
    >> source files rooted anywhere you like (ie the paths don't have to
    >> replicate the package names). The only constraint is that if the source
    >> files depend on classes which are not being named explicitly, then
    >> either their class files must be on an explicitly-given classpath, or
    >> their source files must be on an explicitly-given source path.

    >
    > Yes, I know but for the OP who is obviously a beginner I was trying to keep
    > it simple and point him towards the simplest use case.


    Oh, okay. In that case, the word you're looking for is "should", not
    "must".

    > He should use an IDE anyway.


    Perhaps. Those are pretty confusing for beginners too.

    tom

    --
    There is a faster way to find out...
    Tom Anderson, Jan 19, 2010
    #11
  12. On Tue, 19 Jan 2010 17:39:57 +0000, Tom Anderson wrote:

    > On Mon, 18 Jan 2010, Silvio wrote:
    >
    >> "Tom Anderson" <> wrote in message
    >> news:...
    >>> On Sat, 16 Jan 2010, Silvio wrote:
    >>>
    >>>> A final remark: you must run javac from the root of your source tree.
    >>>
    >>> Not so. You can run it from anywhere you like, and pass in paths to
    >>> source files rooted anywhere you like (ie the paths don't have to
    >>> replicate the package names). The only constraint is that if the
    >>> source files depend on classes which are not being named explicitly,
    >>> then either their class files must be on an explicitly-given
    >>> classpath, or their source files must be on an explicitly-given source
    >>> path.

    >>
    >> Yes, I know but for the OP who is obviously a beginner I was trying to
    >> keep it simple and point him towards the simplest use case.

    >
    > Oh, okay. In that case, the word you're looking for is "should", not
    > "must".
    >
    >> He should use an IDE anyway.

    >
    > Perhaps. Those are pretty confusing for beginners too.
    >

    I'm pleased to say that I learned to use naked javac and java with a text
    editor and command line as my first Javan exposure, then added javadocs,
    followed by jar and ant and only after that used an IDE.

    I think it helps to know how all these bits fit together before getting
    exposed to what is a pretty daunting mosaic of panels and lists of
    'stuff'. The only place where I find an IDE has a major advantage is in
    refactoring. For almost everything else I'm more than happy to stick to
    the command line, though I must admit it helps a lot that its a bash
    command line rather than one powered by cmd.exe. If it was the latter I
    might well be keener on using IDEs.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Jan 20, 2010
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gk
    Replies:
    7
    Views:
    411
    Daniel Pitts
    Nov 4, 2006
  2. Nagaraj
    Replies:
    1
    Views:
    854
    Lionel B
    Mar 1, 2007
  3. Zorg
    Replies:
    0
    Views:
    524
  4. Srini
    Replies:
    11
    Views:
    967
    Arne Vajhøj
    Jun 1, 2008
  5. Rhino
    Replies:
    4
    Views:
    351
    markspace
    Feb 14, 2010
Loading...

Share This Page