[Webinar] Streamline your web hosting managementRegister Today



Posted on 2000-01-06
Medium Priority
Last Modified: 2010-04-01
Im writing a Java Application and I wanted to create a Package so since the name of my main class is called Messenger I called the Package MessengerPack.  I created a directory called MessengerPack and put my files (2 of them) inside of the directory.  

At the top of both files I put...

package MessengerPack;
import java.awt.*;
import java.awt.event.*;

I setup my Classpath to include the Java run time interpreter's directory and the folder that contains my MessengerPack folder.

So d:\studio\myprojects\MessengerPack
set ClassPath=d:\studio\myprojects

Everything compiles OK but when I run my application the Interpeter cannot find the Messenger class.

I can't seem to figure out why it wont find it.  I tried running the program from inside the MessengerPack folder with no luck.  I get the exception...

java.lang.NoClassDefFoundError: Messenger
Exception in thread "main" Exit code: 1
There were errors

If anyone knows what I missed I could use the help.  Im using jdk 1.2.2
Could it be that Im using the wrong path to the JRE?


Question by:dmaroff
  • 3
  • 3
  • 2

Expert Comment

ID: 2331585
are u using
java MessengerPack.Messenger

Accepted Solution

vichu earned 400 total points
ID: 2331600
Try 1:
      Go to the directory holding the
     Eg.  d:\studio\myprojects
      Execute the application,
  java MessengerPack.Messenger.

Try 2:
       Please recheck the classpath


Author Comment

ID: 2332289
Vichu, what would I need to put in the classpath so I can just type:

java Messenger
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Expert Comment

ID: 2332994
there's no way...
Messenger is part of the package MessengerPack that's y u need to invoke the class like that...
u could remove Messenger from MessengerPack...

Expert Comment

ID: 2334149
lychee's comment is correct.We have to
execute the application like that only
because the class Messenger is a part of
the MessengerPack package.If you still
want to execute your application like
java Messenger, remove the Messenger class from the package by removing the
"package Messenger;" in your code.
I think, the above will solve your problem.


Author Comment

ID: 2334284
Thanks, I upped the points to to 150 cause you've been a big help.  I just have 2 last questions, if you don't mind.  

In C++ I can have seperate .c files and compile them together into 1 executable.  I want to have the same in my java program, but the only way I know how is through making a package and putting all the files in the package.  Every class that I made has its own file and at the top of it has the "package MessengerPack", is this the correct way to go about this?  I might end up with 6 different classes, but I don't want 1 huge file.  Thats the only reason I used a package in the first place.  

Also if I want to distribute my program, does the person's classpath variable have to be set like mine, and would it be any easier to just package all the class files into a .jar file so it would execute like one big executable?

Thanks for all your help,

Expert Comment

ID: 2334780
the idea of packages is to eliminate class name conflicts (where 2 classes have the same name) and also for a logical grouping of classes... as for putting the files into 1 file, u'd have to use a jar file... for distribution, u should use a jar file, since data compression is used and so it'll be smaller... then for ur clients to use ur program, if they use:

java 1.1:
jre -cp xxxxx.jar Messenger
(if u pulled Messenger into the default package; otherwise it'll still be MessengerPack.Messenger)

java 2:
java -jar xxxxx.jar

u should include a manifest file in ur jar file that includes the line:

Main-Class: Messenger

and ur jar file should contain the same directory structure as ur current development directory, as in u should have a MessengerPack directory in the jar file that contains the classes that belong to the package MessengerPack...

Author Comment

ID: 2334807
But what if the classes have different names so there are no name conflics, do I still need a package?  If not how would I get one file to 'see' the next?  Also is there a good book or web site that explains all of this housekeeping?  It seems now that Java has a lot more housekeeping than C++.

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question