Page 1 of 2 12 LastLast
Results 1 to 20 of 23
Like Tree1Likes

Thread: javac cannot compile my source which uses a package

  1. #1
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default javac cannot compile my source which uses a package

    Hi,
    I'm learning java and cannot step over this problem:

    I have created a new directory to store my packages. This is in my home directory: <myhome>/prog/j/packages and there is a java source file: <myhome>/prog/j/packages/SamplePackedClass.java. This file contains only this:
    Java Code:
    package samplepackage;
    public class SamplePackedClass {
    }
    There is another java source file in <myhome>/prog/j/UsePredefinedPackage.java:
    Java Code:
    import samplepackage.*;
    public class UsePredefinedPackage {
    }
    Tutorials and online helps write about compiling that, I have to create a new environment option to compile it correctly:
    Bash Code:
    export CLASSPATH=[I]<myhome>/prog/j/packages[/I]
    Yeye, but doesn't work:
    Bash Code:
    javac UsePredefinedPackage.java 
    UsePredefinedPackage.java:1: error: package samplepackage does not exist
    import samplepackage.*;
    ^
    1 error
    I tried to use -cp flag also, as can be read in javac manual. Same result.

    I mess somethig, but really do not know what. Can somebody help?

    Another question: if CLASSPATH way is right, how can I give more, than one? Some env option uses ":" to separate, but I don't know about this any more.

  2. #2
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    if u wanted to use more than one environment variable while compiling the code. USE the command set path="ur environment path " and then start executing
    all the files in that location will be available from then. and make sure u use this command when ever u compile ur code

  3. #3
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    if u wanted to use more than one environment variable while compiling the code. USE the command set path="ur environment path " and then start executing
    all the files in that location will be available from then. and make sure u use this command when ever u compile ur code

  4. #4
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by mkarthik90 View Post
    if u wanted to use more than one environment variable while compiling the code. USE the command set path="ur environment path " and then start executing
    all the files in that location will be available from then. and make sure u use this command when ever u compile ur code
    I don't understand you. Please try to write any example.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: javac cannot compile my source which uses a package

    Your package name must be the same as the name of the folder containing it.
    Your .java files are not in the correctly named folders or the package statement is wrong. They must match.

  6. #6
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by Norm View Post
    Your package name must be the same as the name of the folder containing it.
    Your .java files are not in the correctly named folders or the package statement is wrong. They must match.
    I tried, didn't work.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: javac cannot compile my source which uses a package

    If you want help you will have to say more than "it didn't work".
    What you have posted so far will not work. You need to change it. What have you changed it to?
    What errors are you getting?

  8. #8
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by Norm View Post
    If you want help you will have to say more than "it didn't work".
    What you have posted so far will not work. You need to change it. What have you changed it to?
    What errors are you getting?
    I tought, it is unambigous. Sorry.
    I changed the name of the folder from packages to samplepackage (strange!!!). The result is completely the same as before.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: javac cannot compile my source which uses a package

    I have no idea of what you are currently working with. There are folders, source files, classpath settings, the current folder when you execute the javac command, the commandline, etc
    Somewhere in all those different files, settings etc things are not right. Unless you completely describe it all, there is no way to guess the dozens of different ways it could be wrong.

  10. #10
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by Norm View Post
    I have no idea of what you are currently working with. There are folders, source files, classpath settings, the current folder when you execute the javac command, the commandline, etc
    Somewhere in all those different files, settings etc things are not right. Unless you completely describe it all, there is no way to guess the dozens of different ways it could be wrong.
    Everything is written in this topic. There are no any other settings lines, characters. Nothing. I gave everything.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: javac cannot compile my source which uses a package

    Post the console contents from when you compile. The folder name, the contents of the folder (use dir command) and the commandline

  12. #12
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by Norm View Post
    Post the console contents from when you compile. The folder name, the contents of the folder (use dir command) and the commandline
    OK. I do not understand, because... it works. I've restarted system and it is working now. javac compiled my new class... Very strange. I have to know the reason of this effect to keep clear of this mistake. It is my mistake, but still I do not know where. Can you explain to me?

    It is just for mystify me:
    Bash Code:
    javac UsePredefinedPackage.java
    (Notice please, I didn't use -cp path option.)
    This file is in my <myhome>/prog/j folder and compiled.
    The package is <myhome>/prog/j/samplepackage/SamplePackedClass.java (I have precompiled it previously and <myhome>/prog/j/samplepackage/SamplePackedClass.class file exists of course.)
    And now I want to see my CLASSPATH env option:
    Bash Code:
    echo $CLASSPATH
    (nothing)
    Regenerating CLASSPATH results compiler error again:
    Bash Code:
    javac UsePredefinedPackage.java 
    UsePredefinedPackage.java:1: error: package samplepackage does not exist
    import samplepackage.*;
    ^
    1 error
    I mess something, sure. Need still help. :(
    Last edited by jpocok; 02-09-2012 at 09:24 AM.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: javac cannot compile my source which uses a package

    Post the console contents from when you compile. The folder name, the contents of the folder (use dir command) and the commandline.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by jpocok View Post
    Regenerating CLASSPATH results compiler error again:
    Bash Code:
    javac UsePredefinedPackage.java 
    UsePredefinedPackage.java:1: error: package samplepackage does not exist
    import samplepackage.*;
    ^
    1 error
    I mess something, sure. Need still help. :(
    You should be using -cp, not a CLASSPATH variable (unless it is to "populate" the -cp switch in a batch file).
    IN any case, you haven't told us what value you are putting into the CLASSPATH.

  15. #15
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by Norm View Post
    Post the console contents from when you compile. The folder name, the contents of the folder (use dir command) and the commandline.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    I use linux, not DOS, but have written my folder tree.
    I've already pasted everything several times.
    Thank you for help.
    Last edited by jpocok; 02-09-2012 at 09:37 PM.

  16. #16
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by Tolls View Post
    You should be using -cp, not a CLASSPATH variable (unless it is to "populate" the -cp switch in a batch file).
    IN any case, you haven't told us what value you are putting into the CLASSPATH.
    CLASSPATH is set to <myhome>/prog/j/samplepackage
    Of course I tried -cp option this way:
    Bash Code:
    javac -cp <myhome>/prog/j/samplepackage UsePredefinedPackage.java
    Same error.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default Re: javac cannot compile my source which uses a package

    As a rule of thumb: javac compiles files while java runs classes (optionally in a .jar file). If you want to compile a file named UsePredefinedPackage.jave you'd better make sure that file is in your current working directory; otherwise you have to supply the path to that file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Quote Originally Posted by JosAH View Post
    As a rule of thumb: javac compiles files while java runs classes (optionally in a .jar file). If you want to compile a file named UsePredefinedPackage.jave you'd better make sure that file is in your current working directory; otherwise you have to supply the path to that file.

    kind regards,

    Jos
    I'm using another way to store. Thanks for help. I will solve it.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: javac cannot compile my source which uses a package

    I've already pasted everything several times.
    I didn't see the console contents that shows:
    1) the current directory that the command was issued in
    2) the contents of the current directory

    On windows this is what is shown:

    Microsoft Windows XP [Version 5.1.2600]

    D:\JavaDevelopment\Testing\ForumQuestions\rwm3>dir
    Volume in drive D is Work
    Volume Serial Number is 0C24-B38C

    Directory of D:\JavaDevelopment\Testing\ForumQuestions\rwm3

    09/11/2009 01:35 AM <DIR> .
    09/11/2009 01:35 AM <DIR> ..
    07/04/2001 05:21 PM 20,768 BB.gif
    06/09/2001 01:17 AM 695 Comp.java
    06/30/2001 12:40 AM 885 Dot.gif
    05/15/2001 01:46 AM 2,419 R1.gif
    07/20/2001 10:30 PM 1,027 rwm3.html
    07/21/2001 10:07 AM 7,784 rwm3.java
    6 File(s) 33,578 bytes
    2 Dir(s) 7,023,009,792 bytes free

    D:\JavaDevelopment\Testing\ForumQuestions\rwm3>
    The Orange is the current directory path. Below is the contents of that directory.

  20. #20
    jpocok is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: javac cannot compile my source which uses a package

    Forget it, please. I'm going to solve the problem alone. Many thanks for all.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 11
    Last Post: 01-04-2012, 08:00 PM
  2. Replies: 7
    Last Post: 08-29-2011, 06:35 PM
  3. javac does not import package from jar in 1.6
    By tonydr in forum Advanced Java
    Replies: 0
    Last Post: 05-26-2010, 10:34 PM
  4. error while compile code using javac
    By suri in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:10 AM
  5. Compile and Run a Java package
    By hussain in forum New To Java
    Replies: 1
    Last Post: 09-10-2009, 09:15 AM

Posting Permissions

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