Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Import and package

    I still dont get how this really works in a way. I have read about this on a few sites but it still does not answer my question.

    I have a program, it contains 3 class files, one of these contain a main method.
    All these classes are located in the same folder, called Crypt.
    Here is the code of the class file.

    Java Code:
    package Crypt;
    import Crypt;
    
    class mainprogram
    {
    	public static void main..........
    Java Code:
    package Crypt;
    import Crypt;
    
    public class cryptMethod
    {
    ...
    Java Code:
    package Crypt;
    import Crypt;
    
    public class SuperObj
    {
    ...
    So when I try to compile, I get error saying that it cant find each others methods. How come?

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Package names are case sensitive, and lower case one the first letter, is convention, if not on all.
    It is also convention to name the package as the domain (in reverse order) you have uploaded it on (your website),
    so that it can be unique world wide.
    So if your application is named ABC, and your website is www.example.com, than you may want to use the package name: com.example.abs [www is excluded since it purpose is to make it easier for users to specify protocol, like HTTP in this case, it is however not strict like http://]
    Dots in the package name indicates new folder in the file system.

    The default package is the unnamed package, i.e. the folder you started Java from when running, where you put you packages. The default packages can only be reached from itself, so package crypt can not access classes in the default packages. Otherwise all packages can access all packages.

    To select package the main class [the class whose method ”main” should be started] you add the parameter you just att the package [here com.example.abc] name in front of the class name [here Program], like com.example.abc.Program instead of Program. I should also use the parameters [-classpath .] or [-cp .] when you are starting or compiling the program.

    If a file in package p0 should be able to access all public classes [&c] in another package p1, you most in that file (by convention directly beneath package p0); add the statement import p0.*; There are other ways of importing as well if you are interested in learning more.

    Files in a package must not import their own package. So a file cannot contain the lines [at the same time]:

    Java Code:
    package Crypt;
    import Crypt.*; //import Crypt;
    Since it is automatically imported.
    Last edited by Hibernate; 08-06-2011 at 11:14 PM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    when I try to compile, I get error
    Please copy and paste the full text of the error message here.
    When you are compiling, does the class path point to the folder containing the package path folder: Crypt?
    One easy way is to use ..\. to refer to the folder up one level. For example:

    D:\Java\jdk1.6.0_25\jre\bin\java.exe -Xmx512M -classpath D:\JavaDevelopment;.;..\. TestCode5

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    But -Xmx does not really work in Windows (know idea about Java 7), does it?

    @OP
    I assume your class is in the file folder Crypt.
    However what is [probably] wrong is two things:


    Syntax:
    import Crypt;
    should be
    import Crypt.*;

    Self importing:
    package Crypt cannot import the package Crypt, it is already imported, automatically.


    So solution:

    Remove the line:
    import Crypt;



    And how you compile:

    Open a console and cd (best practice) to the folder contain the folder Crypt.

    Further type (to compile), this assumes all your classes are located in the folder Crypt and that you use the default file extension .java, and not .jav which is also allowed:
    javac -classpath . Crypt/*.java

    Crypt/*.java means all in the folder Crypt ending with .java, and I believe it is allowed syntax in Windows' console prompt.
    (Windows threats / as \ in filenames, and tries and is case insensitive and will thread maiN.JaVa as Main.java if Main.java is the file with most similar casing, so *.java will also find *.JAVA).

    And to execute the program:

    java -cp . Crypy.mainclass
    (or javaw, I don't remember, which is which, but I think javaw is the consoleless one.)


    Also it is convention to have camelcase with uppercase on the first letter in class names (MainClass, rather then mainclass).
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

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

    Default

    I agree with Norm - the exact and entire compiler message would help a lot. To make sense of it we also need the code it is compiling and, since there are various classes not being found, it would be a good idea to cut the problem down to the smallest possible number of classes which show this problem when you compile them.

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Ai, but not why not help him with what looks wrong, and the basics, till he wakes up.

    @Pojahn_M: You did not happen to make the methods private (which only happens if the use the 'private' keyword on the method's heads)?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default

    Quote Originally Posted by Hibernate View Post
    But -Xmx does not really work in Windows (know idea about Java 7), does it?
    Of course it does. How else do you tell the JVM how much memory to use?
    Not that Xmx is needed here, of course...

  8. #8
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Little improvement, but still not working!
    There are 3 classes, mainprogram(main/client class) is located in folder crt.
    The other 2 classes are located in crt/crypting/tools.

    This class compile and is also the main class:
    Java Code:
    import java.util.*;
    import crypting.tools.*;
    import java.io.*;
    
    class mainprogram
    {
    //Class create a object of SuperObj and use methods from cryptMethods
    ...
    This class compile:
    Java Code:
    package crypting.tools;
    
    import java.util.*;
    import java.io.*;
    import java.lang.*;
    
    public class cryptMethods
    {
    //This class does not use anything from mainprogram or SuperObj
    ...
    This class does not compile:
    Java Code:
    package crypting.tools;
    
    //import crypting.tools.*; Its not required to import it, it is automatically imported
    import java.util.*;
    import java.io.*;
    
    public class SuperObj
    {
    	private String[][] key;
    	private String filePath;
    
    	public SuperObj ()
    	{
    		key = null;
    		filePath = null;
    	}
    
    	public void setKey (String key[][])//Chaning key will also change this.key, since they point to the same place in the computer memory, but this wont be an issue in this program.
    	{
    		this.key = key;
    	}
    
    	public void setFilePath (String filePath)
    	{
    		this.filePath = filePath;
    	}
    
    	public void encryptFile () throws Exception //Call an other method to do the dirty job
    	{
    		cryptMethods.encryptFile (this.key, this.filePath);
    	}
    
    	public void decryptFile () throws Exception //Call an other method to do the dirty job
    	{
    		cryptMethods.decryptFile (this.key, this.filePath);
    	}
    }
    The error message when SuperObj try to compile.
    Java Code:
    C:\Documents and Settings\Pojahn\Skrivbord\crt\crypting\tools\SuperObj.java:30: cannot find symbol
    symbol  : variable cryptMethods
    location: class crypting.tools.SuperObj
    		cryptMethods.encryptFile (this.key, this.filePath);
    		^
    C:\Documents and Settings\Pojahn\Skrivbord\crt\crypting\tools\SuperObj.java:35: cannot find symbol
    symbol  : variable cryptMethods
    location: class crypting.tools.SuperObj
    		cryptMethods.decryptFile (this.key, this.filePath);
    		^
    2 errors
    
    Tool completed with exit code 1

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    cannot find symbol
    symbol : variable cryptMethods
    Where is the class(???Class names should start with uppercase) cryptMethods defined?

    Where does the classpath point? It must point to the folder that contains the crypting folder which must contain the tools folder which contains the classes.

  10. #10
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Fixed so it start with uppercase(I have never had problem with the class name starting with lower case).
    cryptMethod(now changed to CryptMethods)is located in crt/crypting/tools, SuperObjs as well, so they are in the same directory.

    I dont understand your last sentence.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    What paths are in your classpath when you compile the program?

    The classpath must point to the folder that contains the crypting folder which must contain the tools folder which contains the classes.
    SomeFolder\crypting\tools\<CLASS FILES HERE>

    The classpath should contain the path to: SomeFolder
    java -cp C:\Java\SomeFolder;.
    Last edited by Norm; 08-08-2011 at 07:35 PM.

  12. #12
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    Of course it does. How else do you tell the JVM how much memory to use?
    Not that Xmx is needed here, of course...
    I read (in a Official Sun article I believe) that it does not work for Window, but that it did for GNU/Linux (remember Java 6), and that there is no way to tell the JVM how much memory to use in Windows.
    However it did not warn about that function being infunctional.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  13. #13
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    creating classpath does not work for some reson.
    First, I moved all java files to a folder called crt.
    Then I open cmd and wrote cd C:\Documents and Settings\Pojahn\Skrivbord\crt

    Then:

    Java Code:
    C:\Documents and Settings\Pojahn\Skrivbord\crt>C:\Program\Java\jdk1.6.0_23\bin\javac.exe -classpath . crt/*.java
    javac: file not found: crt\*.java
    Usage: javac <options> <source files>
    use -help for a list of possible options
    I tried some different things, but it cant find any files.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    javac: file not found: crt\*.java
    crt\*.java does not look like the name of a file. The javac command does not think it is the name of a file.
    Have you tried the javac command with a single file instead of using a wildcard?

    javac <A GOOD FILENAME>.java

  15. #15
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    Yes, I have tried a single file.

    Java Code:
    C:\Documents and Settings\Pojahn\Skrivbord\crt>C:\Program\Java\jdk1.6.0_23\bin\javac.exe -classpath . crt\Mainprogram.java
    I get the same error message as I posted above. Very odd...

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    Try moving to the folder with the source file so there is no need for the path to the source following the javac command.

  17. #17
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default

    that did not work. I also tried with any <option>, it compiled the file, but after that, javac.exe got corrupted, I have to reinstall the whole thing.
    btw, if I put all my classes in a jar, do I need to import them(and all that stuff) or will they read all classes inside the jar?

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

    Default

    Quote Originally Posted by Hibernate View Post
    I read (in a Official Sun article I believe) that it does not work for Window, but that it did for GNU/Linux (remember Java 6), and that there is no way to tell the JVM how much memory to use in Windows.
    However it did not warn about that function being infunctional.
    Then I suspect you read wrong, as it does work and has done since 1.2.
    I think I'd have noticed otherwise.
    Java Code:
    public class Scratch {
        ArrayList al;
        public static void main(String[] args) {
            int count = 0;
            {
                List<Scratch> stuff = new ArrayList<Scratch>();
                while (true) {
                    stuff.add(new Scratch());
                    System.out.println(++count);
                }
            }
        }
        public Scratch() {
            al = new ArrayList();
            for (int i = 0; i < 1000; i++) {
                al.add(new Object());
            }
        }
    }
    That gets to 2100ish for Xmx64m on 1.5 and 1.6 (and probably earlier ones as well), and 4200ish for Xmx128m.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    javac.exe got corrupted, I have to reinstall the whole thing.
    I have Never heard of that happening. What did you do to corrupt the javac.exe file?

    if I put all my classes in a jar, do I need to import them(and all that stuff) or will they read all classes inside the jar?
    The jar file will need to be on the classpath for the javac and java commands to find any classes that are in the jar file.
    The import statement tells the javac command where to look for class definitions when looking on the classpath.

  20. #20
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    Then I suspect you read wrong, as it does work and has done since 1.2.
    I think I'd have noticed otherwise.
    Java Code:
    public class Scratch {
        ArrayList al;
        public static void main(String[] args) {
            int count = 0;
            {
                List<Scratch> stuff = new ArrayList<Scratch>();
                while (true) {
                    stuff.add(new Scratch());
                    System.out.println(++count);
                }
            }
        }
        public Scratch() {
            al = new ArrayList();
            for (int i = 0; i < 1000; i++) {
                al.add(new Object());
            }
        }
    }
    That gets to 2100ish for Xmx64m on 1.5 and 1.6 (and probably earlier ones as well), and 4200ish for Xmx128m.

    Can you go over the standard value?
    And what is the standard value?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Page 1 of 2 12 LastLast

Similar Threads

  1. Import your own package
    By Umi in forum New To Java
    Replies: 4
    Last Post: 02-15-2010, 09:05 AM
  2. How to import a Java package?
    By tyang in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 09:14 PM
  3. Help Needed to import package
    By gradStudent in forum New To Java
    Replies: 10
    Last Post: 08-11-2009, 01:37 AM
  4. How to import a package before it is compiled
    By gradStudent in forum Advanced Java
    Replies: 1
    Last Post: 08-11-2009, 12:17 AM
  5. How to import a package in Eclipse?
    By naipulb in forum New To Java
    Replies: 2
    Last Post: 06-09-2008, 11:04 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
  •