aspose file tools*
The moose likes Beginning Java and the fly likes making a small package example work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "making a small package example work" Watch "making a small package example work" New topic
Author

making a small package example work

Edwin Davidson
Greenhorn

Joined: Nov 26, 2003
Posts: 27
I need a small push in understanding packages.My CLASSPATH variable was set to e:\java;. - I changed it for this to e:\java;.;e:\justjava2\chap09
in e:\justjava2\chap09 I have mathpackage.java - code is here:
*************************************************
package math;
class mathpackage
{
public static void main(String v[])
{
int a,b;
a=b=10;
mathlibrary ml=new mathlibrary();
System.out.println(ml.multiply(a,b));
}
}
**************************************************
in e:\justjava2\chap09\math I have mathlibrary.java - code is here:
**************************************************
class mathlibrary
{
public int multiply(int a, int b)
{
return a*b;
}
}
***************************************************
When I compile mathpackage.java I get this:
E:\justjava2\chap09\mathpackage.java:9: cannot access math.mathlibrary
bad class file: E:\java\math\mathlibrary.class
class file contains wrong class: mathlibrary
Please remove or make sure it appears in the correct subdirectory of the
classpath.
mathlibrary ml=new mathlibrary();
^
1 error
I am compiling with 'javac mathpackage'. You'd really would think that something like this would work straight off. This just can't be that hard. Likely some small silly thing I'm doing wrong. I've tried using a
"import math.*;" statement in mathpackage.java but no use. And is there a site that CLEARLY explains how to setup and properly use packages ? Again - help please.
Thank you.
Edwin
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Your mathlibrary needs the

and mathpackage doesn't.
Note: Avoid confusing names like 'MathPackage'.
It's you Application, so MathApplication might be a choice - or MathApp.
if you start from dir .../SOME
Let this be the root of your world.
If you create a package, it has to be in a directory of the same name:

If a class Foo claims to be in a package x, it has to be in a directory x.
If class Bar claims to be in x.y it has to be in directory x/y

Note: On Windows, you need falling slashes.
Note: package-names should be in all lowercase letters (ie.: java.beans.beancontext).
Note: Class-names should start with uppercase, and use uppercase for combined words repeatedly (ie.: MouseListener).
Note: To access 'Bar', you need:


The example above will still not work, because if you import own or third-party packages, your classes need package-declarations too.
Declare your App to be in the package math, and move it there:

But don't 'cd' to 'math'. We all know, it seems to be easier moving to math, to avoid tipping 'math/' all the time ('edit math/MathApp.java', 'javac math/MathApp.java', 'java math/MathApp') but if you move to 'math', you get short commands and big trash.
source:
For your source code:
- for very small projects (2 - 3 classes) you may mix .java and .class files in the same directory.
- for bigger projects, you make two roots, one for the source, one for the bytecode.

to compile, you simply move to

Note: Avoid moving to x/y/ to compile Bar.java.
It is x.y.Bar, and you have to compile it from .../src .
to pack it into an jar, you move to the PARENT of bin (often: classes)

Splitting source and classes is useful for:
- creating jar's that don't contain the source
- to ship the classes to a customer without the sources
- to backup the sources without the classes
Creating a jar is very useful, since this jar will now be the root of your world, and you may call it from where you want, (if you generated a manifest with the main-class):
.../FAR/far/away> java -jar ../../../A/B/c/Math.jar
Note: It's generally a bad idea, to use the JDK-DIR as source of your classes/ projects/ sources.
If you have:

and get the new j2sdk1.4.2, you probably have to fix a lot of scripts.
---
[ March 31, 2004: Message edited by: Stefan Wagner ]
edited to add a missing hint, minor improvements.
[ April 01, 2004: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Edwin Davidson
Greenhorn

Joined: Nov 26, 2003
Posts: 27
My CLASSPATH is still set to e:\java;.;e:\justjava2\chap09
OK I renamed e:\justjava2\chap09\mathpackage.java to e:\justjava2\chap09\mathapp.java and the code is below:
class mathapp
{
public static void main(String v[])
{
int a,b;
a=b=10;
mathlibrary ml=new mathlibrary();
System.out.println(ml.multiply(a,b));
}
}
in e:\justjava2\chap09\math\ I have mathlibrary.java - code is here:
class mathlibrary
{
public int multiply(int a, int b)
{
return a*b;
}
}
I compile these 2 files like so:
e:\justjava2\chap09>javac mathapp.java
e:\justjava2\chap09>javac math\mathlibrary.java
And they both compile clean and without error. Now I am getting this error when I type "javac mathapp":
E:\justjava2\chap09>java mathapp
Exception in thread "main" java.lang.IllegalAccessError: tried to access class mathlibrary from class mathapp
at mathapp.main(mathapp.java:10)
Can you please tell me what I am doing wrong ?
Thank you very much.
Edwin
Amit Ghai
Ranch Hand

Joined: Mar 24, 2004
Posts: 35
i doubt that your mathapp compiled. You have to write package math on top of mathlibrary and also define it as public. then you will have to import math.mathlibrary or use mathlibrary in mathapp as math.mathlibrary.
like this
package math;
public class mathlibrary
{
public int multiply(int a, int b)
{
return a*b;
}
}

import math.mathlibrary;
class mathapp
{
public static void main(String v[])
{
int a,b;
a=b=10;
mathlibrary ml=new mathlibrary();
System.out.println(ml.multiply(a,b));
}
}


Reasonable people adapt themselves to the world. Unreasonable people attempt to adapt the world to themselves. All progress, therefore, depends on unreasonable people.
Edwin Davidson
Greenhorn

Joined: Nov 26, 2003
Posts: 27
This works and makes a ton of sense too - now I'm getting it !! Thank you very much Amit !
Edwin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: making a small package example work