A little confused about packages...
27/01/2018 12:08
I'm writing a program in an IDE (jbuilder 2005), but when I try tocompile it I get errors about classes not being found. I figure outthat it has something to do with the package, so I test it out using asmaller version.


Here is what my directory structure looks like:

~/devel/java/TestPkg/TestPkg.java~/devel/java/TestPkg/TestClass.java
TestPkg.java is:

package TestPkg;public class TestPkg { public static void main(String[] args){ System.out.println(Test); }}
TestClass.java is:

package TestPkg;public class TestClass { TestClass() { System.out.println(TestClass); }}
When I run the compiler everything is fine, but when I try to run iteverything goes nuts! If I remove the package declaration from thebeginning of TestPkg.java then everything works just fine. Is theresome key point I'm missing here???

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
27/01/2018 12:08 - Hi,
What do you type to run it?
cheers,
//mikael

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
27/01/2018 12:08 - <jon23d@gmail.com> wrote...


When you use packages, the package name is a part of the full class name.


Let's say that you are in the directory
~/devel/java/TestPkg/
...when you compile with
javac TestPkg.java
This will create the following class file:

~/devel/java/TestPkg/TestPkg.class
Which means that you will have to move up one directory:

~/devel/java/
...to be able to execute it with:

java TestPkg.TestPkg
This is because the packages in Java are used for both logical and physical structures.


The package name is in Java also a designator of the physical structure, how to organize classes on the disk or in a jar, but as they are foremost a designator in order to organize them logically according to their use, you need the full name (incl. package name), when using them.


That is why you use import for some classes in other packages (the logical organization), but might also need -cp in order to find them (the physical organization).


When you compile and execute from within an IDE, you should be especially careful, as most of such IDEs also creates the directory structure when finding out that a class is within a package.


// Bjorn A
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

Source is Usenet: comp.lang.java.help
Sign in to add a comment

Answer score: 5
27/01/2018 12:08 - javac TestPkg.java <--- this works finejava TestPkg <---- this doesn't!
This is what I've used up until I started using packages, and hasworked fine...



Source is Usenet: comp.lang.java.help
Sign in to add a comment

eDiscover
Helpforce eDiscover provides technical articles updated each dayHelpforce eDiscover RSS feed contains the latest technical articles in RSS
Click the logo to go back to the main page
Search eDiscover
  
Categories

Click an icon to go to that category

Helpforce eDiscover contains articles about Microsoft Windows Helpforce eDiscover contains articles about Apple products and MacOS Helpforce eDiscover contains articles about Linux and POSIX operating systems Helpforce eDiscover contains articles about Helpforce Helpforce has a large variety of technical information and articles for you to read Helpforce eDiscover contains articles about databases, MYSQL, SQL Server Oracle Helpforce eDiscover contains articles about Java, JVM and the JRE Helpforce eDiscover contains articles about the QNX operating system Helpforce eDiscover contains articles about Oracle Solaris and Open Solaris Helpforce eDiscover contains articles about RISC OS, Acorn and the BBC Micro Helpforce eDiscover contains articles about Amiga and AmigaOS

Type your comment into the box below