Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packages

 
Amna Arif
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to create packages in java? I have a problem in setting CLASSPATH variable and I also have problem in compiling programs in a package.
 
peter greaves
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
packages are created (logically)as they are declared, using the "package" keyword. the JLS governs that. the compiler will create a directory structure that "reflects" your package names. maybe you could post the errors you are getting with yr code, so we could help out?
regs
peter
 
Amna Arif
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
whenever i try to compile a class file in a package after setting CLASSPATH i get the following error message
E:\jdk1.4\bin\lab>javac toy.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
E:\jdk1.4\bin>javac toy.java
error: cannot read: toy.java
1 error
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'javac' is not recognized as an internal or external command,
operable program or batch file.

This is a PATH issue, not a CLASSPATH issue. The CLASSPATH is concerned with locating Java class files. The PATH is concerned with locating executables, like .exe files.
You probably want to adjust your PATH setting to include the location of javac.exe (which is included with your Java 2 SDK installation).
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E:\jdk1.4\bin>javac toy.java
error: cannot read: toy.java

Where is toy.java located? It must not be in the E:\jdk1.4\bin directory. Is it in the E:\jdk1.4\bin\lab directory? If it is, then try E:\jdk1.4\bin>javac lab\toy.java
Note that the CLASSPATH setting isn't concerned with locating Java source files, it specifies the location of compiled classes.
 
Amna Arif
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if there is a class file in a package. To execute it from the package iam setting CLASSPATH but it is not working. The lab package is in JDK1.4\bin\lab. Please help.
 
Gayathri Prasad
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
After u set the path for javac compiler and the following is the procedure I used for compiling and accessing my java files in the package.
I have a folder in the F:\Programs\Java\mypack
The CLASSPATH is set to F:\Java\Programs (i.e the classpath has to be set to the parent folder in which the package resides i.e to the F:\Java\Programs because the mypack folder resides in F:\Java\Programs\mypack )
To compile them u need to compile them in the following way.
F:\Programs\Java>javac mypack\*.java
Cheers,
Gaya3
-------------------------------------------------
Beginning is half done
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic