Solved

Setting Classpaths and importing packages

Posted on 2003-11-19
28
1,152 Views
Last Modified: 2013-11-23
I am new to java programming and currently i am using j2sdk1.4.2 compiler. Each time I try compilling or running a program, i encounter several errors that has to do with classpath setting.

I wrote this two classes:

this class resides in c:\packages\classxmp

// Calc class
package classxmp;

public class Calc
{
 public int area(int x, int y)
 {
   int sol;
   sol = x*y;
   return sol;
 }
}


this class also resides in c:\packages\classxmp
//  Trial Class
package classxmp;

import classxmp.*;

public class Trial
{
 int x = 5;
 int y = 6;
 int a;
 public void main(String args[]) {
 Calc dl = new Calc();
 a = dl.area(x,y);
 System.out.println("area is: "+a);
 }
}

after compilling these two classes, each time I try running the program by issuing this command.

C:\>c:\j2sdk1.4.2\bin\java -classpath . packages.classxmp.Trial
 
I get this error message below.

Exception in thread "main" java.lang.NoClassDefFoundError: packages/classxmp/Trial (wrong name: classxmp/Trial)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:537)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

Please help me, I don't understand what this means.
this classpath thing is driving me nuts.


Raymond
Lagos, Nigeria
Rayasadu@yahoo.com
0
Comment
Question by:fox4life
  • 11
  • 11
  • 3
  • +2
28 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9781629
Move to directory called 'packages' and do

javac classxmp\Calc.java

and like for the other. The java bin directory should be on your PATH varaiable
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 9781643
You could also stay where you are and do

javac -classpath c:\packages classxmp\Calc.java

or wherever your packages directory is. The previous method is probably better though
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9781652
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 86

Expert Comment

by:CEHJ
ID: 9781686
You shouldn't really have classes under your bin directory if that's where they are. Delete them and start again. With the command line mentioned earlier. You don't need to import classes in the same package so delete

>>import classxmp.*;

0
 
LVL 92

Expert Comment

by:objects
ID: 9781861
Just add c:\packages to your classpath
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 9781871
> java -classpath . packages.classxmp.Trial

You are also using the wrong command, try:

java -classpath c:\packages classxmp.Trial

0
 
LVL 92

Expert Comment

by:objects
ID: 9781885
> I don't understand what this means.

It means it cannot find the specified class.

> this classpath thing is driving me nuts.

The classpath is used by the class loader to locate classes. It is a list of directories and jars files, that works similarly to the PATH variable for locating executables. When a class is requested, each entry in the classpath is used to search for the specified class.
0
 
LVL 92

Expert Comment

by:objects
ID: 9781897
> javac -classpath c:\packages classxmp\Calc.java

All that typing is not really necessary, you can simply go to the directory c:\packages\classwmp and use:

javac Calc.java


0
 
LVL 16

Expert Comment

by:gnoon
ID: 9784602
Your two classes are member of classxmp package, so when you compile them, should use
    javac Calc.java -d .     and    javac Trial.java -d .     or
    javac *.java -d .
You will get a folder called 'classxmp' and the .class files.

Then, run the class with this command
    java classxmp.Trial

All above commands should be executed at the directory wherever .java files are there.
0
 
LVL 92

Expert Comment

by:objects
ID: 9784633
That'll result in a pretty messy directory structure which will quickly become hard to manage.
Easier to put the class files in the same directory as the source, or in a seperate directory structure.

And if you want to avoid using "-classpath c:\packages" then just add that directory to your CLASSPATH environment variable.
0
 
LVL 2

Expert Comment

by:TallBoy
ID: 9788418
Note that the CLASSPATH is pretty much deprecated, as it was unwieldy to begin with.

For jars and the like, the best solution is to put them in the ${j2sdk}\jre\lib\ext and ${j2re}\lib\ext directories.
-------------------
FOX4LIFE: the package designator assumes that you are at the top of the hirearchy when you invoke it.

So,

  package com.example.stuff.test;
  class me ...

would assume that the class resides in the following directory:

[root]/
  com/
    example/
      stuff/
        test/
  me.class

When you invoke me.class, you MUST either be in the [root] directory, OR  point to it on your classpath.

For example, suppose [root] was /usr/bob. The following are valid:
  /user/bob> java com.example.stuff.test.me
or
  /> java -cp /usr/bob com.example.stuff.test.me

Windows example: [root] = c:\usr\bob
C:\usr\bob> java com.example.stuff.test.me
or
c:\usr\bob> java -cp c:\usr\bob com.example.stuff.test.me

0
 
LVL 92

Expert Comment

by:objects
ID: 9791741
> Note that the CLASSPATH is pretty much deprecated

Can you point to a reference stating that.
0
 
LVL 2

Expert Comment

by:TallBoy
ID: 9794256
http://java.sun.com/j2se/1.4.2/docs/tooldocs/solaris/classpath.html

Heading "Description," second paragraph, starting with the third sentence.
Also take a look at the fourth paragraph, second sentence.

Cheers
0
 
LVL 92

Expert Comment

by:objects
ID: 9795045
I don't think that implies that the CLASSPATH is deprecated.
If you have a class hierarchy in a specific directory it is a lot easier to specify that directory in the CLASSPATH env var than having to cd to a directory or enter it on the command line every time you want to use that class. Gets even more annoying when you are compiling code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796263
>>Can you point to a reference stating that.

LOL - i was waiting for you to wade in on this issue ;-) Listening...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796274
>>Note that the CLASSPATH is pretty much deprecated

Let's imagine you'd said 'pretty much to be avoided' TallBoy
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796320
> For jars and the like, the best solution is to put them in the ${j2sdk}\jre\lib\ext and ${j2re}\lib\ext directories.

Gah!!!  My pet peeve!!!

Doing this can just lead you to a world of pain (especially in a proper development environment)...

If you put all the jars you download, or test, or just even look at in the lib/ext directory, when you come to deploy your app on another server, or distribute your app, or upgrade your JVM, you will have no idea which jar is used, by what, or for what...

If you have ever gone to a developer's machine when they are on holiday, and a client is asking for a demonstration off site, only to find the script to run the huge application is:

java Main

when you KNOW it uses a specific version of Xerces, and other libraries, you are left having to copy them all from lib/ext...

This is not a pretty, or fun state of affairs...

Practice with the classpath...  it can be a pain, but when you sort it out, the scripts to run your apps self document which libraries they use...

Start your java apps with a batch file or a script file

Compile your apps with an IDE or ant

Sorry for the rant...

I have just had this problem too many times... :-(

Tim :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796342
My personal take (which is known to many) is that beginners shouldn't be even told that such a thing as CLASSPATH exists ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796350
That's a bit of a "science education" way of looking at things ;-)

Like at A-Level physics, you realise that GCSE physics was all lies and over-simplifications ;-)

Still, you are probably right...

I just don't like the lib/ext method...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796367
Less pain for beginners = fewer easy points for us answering classpath problems ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796375
hehe ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9800287
> Less pain for beginners = fewer easy points for us answering classpath problems ;-)

Educating beginners up front will save a lot of future q's being asked :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9800325
Well I'm not sure where they get their starting advice from or what the advisors are saying. The first thing Java beginners want is to get HelloWorld working right away - they don't need to be wrestling with classpaths. They should be told how to get more information about classpaths later if they have more sophisticated requirements.
0
 
LVL 92

Expert Comment

by:objects
ID: 9800371
> The first thing Java beginners want is to get HelloWorld working right away

I think they move past HelloWorld pretty quickly.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9800397
Don't think so - that's their first brush with classpath misery. Don't forget - they break it because they set the classpath incorrectly - becuase they've been told they need one.
0
 
LVL 92

Expert Comment

by:objects
ID: 9800462
> Don't think so - that's their first brush with classpath misery.

Rarely, it generally happens as soon as there app involves >1 class.

> they break it because they set the classpath incorrectly

Not unless they are taught incorrectly.

> becuase they've been told they need one.

You don't tell them they need one, you teach them how it works.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9800483
Well - next time it happens we'll do some anecdotal research ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 9840010
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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