File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes package statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package statement" Watch "package statement" New topic

package statement

chailin fong

Joined: Oct 15, 2007
Posts: 10

I placed in c:\BookPack,
then I compile this file, it created two .class file

Next, I want to create the package. I go to the directory above BookPack, and issue this command:

C:\> package BookPack

The message shown as:

'package' is not recognized as an internal or external command, operable program or batch file.



package BookPack;

class Book{
private String title;
private String author;
private int pubDate;

Book(String t, String a, int d){
title = t;
author = a;
pubDate = d;

void show(){

class BookDemo{
public static void main(String[] args){
Book books[] = new Book[5];
books[0] = new Book("Java Beginners Guide", "Schildt", 2001);
books[1] = new Book("Java 2 Programmer Reference", "Schildt", 2002);
books[2] = new Book("HTML Programmer Reference", "Powell and Whitworth", 1998);
books[3] = new Book("Red Storm Rising", "Clancy", 1986);
books[4] = new Book("On the Road", "Kerouac", 1995);
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

There's no command package. If you want to create a package, you need to create a directory called BookPack in your case and then put your source code there. Then compile it using the normal javac.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
chailin fong

Joined: Oct 15, 2007
Posts: 10
I have created directory BookPack and place in it.

Now, if I did this:

C:\BookPack> java BookPack.BookDemo

I will get:

Exception in thread :"main" java.lang.NoClassDefFoundError:BookPack/BookDemo
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

go one directory up, and execute the same command.

it should be

C:>java BookPack.BookDemo

[Servlet tutorial] [Servlet 3.0 Cook Book]
bharathibattu kandikuppa

Joined: Mar 16, 2011
Posts: 1
i typed the program like this but it is giving the error 'class' or 'interface' expected what i have to correct the program please reply it i created BookPack directory when i compiled it gives error.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15033

Welcome to the Ranch!

Please UseCodeTags when you post source code, so that your code is formatted nicely with line numbers. I added the code tags in your post above for you.

In which line do you get the error message? Please copy and paste the exact error message here.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: package statement
It's not a secret anymore!