Java : add package /set classpath

I'm new to Java, and I'm working through a course which provides a few class.-files for an easy command-line input and some other stuff.
I've read through a lot of posts and articles on the subject, but still couldn't solve the problem.

First, I want to set an environment variable so the setting will be permanent.

I tried to execute a setenv-command that I've found in ( set CLASSPATH = ($CLASSPATH ~/$HOME/Library/java), I've got an error.

After that, I tried to edit javaconfig.plist, but I got an error message stating I didn't have the privileges to edit this file. (I'm the administrator on my machine.

Someone in this forum posted you could just put the files in the Library/Extensions folder, but the compiler still gives me the package not found-Error.

Now I've put the folder in the same folder where I save my class.-files, that works, but that's hardly a solution.

Any help...please?

Powerbook G4 15" (&iBook clamshell tangerine 300MHz), Mac OS X (10.4.9)

Posted on May 2, 2007 7:56 AM

Reply
16 replies

May 2, 2007 10:13 AM in response to kurt hahn1

Kurt,

Let's say that you have one JAR file that the class files depend upon. Your directory structure might look like this:
<pre>
~/Developement
servlet.jar
com/
sample/
SampleRunner.class
</pre>
Using the Terminal application, change to the Development directory like this:
<pre>cd ~/Development</pre>
Then you could execute this command to run the SampleRunner code:
<pre>java -classpath .:servlet.jar com.sample.SampleRunner</pre>

If you always want to have your classpath be the same, you could run this command from the bash shell:
<pre>export CLASSPATH=~/Development/servlet.jar</pre>

Then, to run your application from the ~/Development directory you could just use the following command:
<pre>java com.sample.SampleRunner</pre>

Hope this helps!

May 4, 2007 12:09 AM in response to kurt hahn1

This little program prints out useful details of your Java system:

public class JavaDetails {
public static void main (String args[]) {
System.out.println ("OS Name " + System.getProperty ("os.name"));
System.out.println ("OS Version " + System.getProperty ("os.version"));
System.out.println ("Architecture " + System.getProperty ("os.arch"));
System.out.println ("Class path " + System.getProperty ("java.class.path"));
System.out.println ("Ext Folders " + System.getProperty ("java.ext.dirs"));
System.out.println ("Java version " + System.getProperty ("java.version"));
}
}

Check that the "Ext Folders" contains /Library/Java/Extensions...

Bob

May 4, 2007 2:11 AM in response to Bob Lang1

Hi,
this is what I got running your program:
OS Name Mac OS X
OS Version 10.4.9
Architecture ppc
Class path .:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibi lity/14compatibility.jar
Ext Folders /Library/Java/Extensions:/System/Library/Java/Extensions:/System/Library/Framew orks/JavaVM.framework/Versions/1.5.0/Home/lib/ext
Java version 1.5.0_07

Although the folder "Prog1Tools" is located in Macintosh HD / Library / Java / Extensions, the import command gives me the error:

book:~/DOcuments/programmation/grundkurs/band1/kap3 kurthahn$ javac HelloXCodeWorld.java
HelloXCodeWorld.java:10: package Prog1Tools does not exist
import Prog1Tools.IOTools;
^
1 error

I was able to compile the code only by putting the folder Prog1Tools in the same directory where HelloXCodeWorld.java is located.

I've tried to set a classpath with the command given in this thread, but I think Iwasn't "at the right place" (in the terminal-window) to do this . This is what I did:

book:~/DOcuments/programmation/grundkurs/band1/kap3 kurthahn$ export CLASSPATH=Users/kurthahn/Library/Java
book:~/DOcuments/programmation/grundkurs/band1/kap3 kurthahn$ javac HelloXCodeWorld.java
HelloXCodeWorld.java:10: package Prog1Tools does not exist
import Prog1Tools.IOTools;
^
1 error

This "export"-command doesn't seem to work, since the new classpath doesn't show with your litte program:

OS Name Mac OS X
OS Version 10.4.9
Architecture ppc
Class path .:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/.compatibi lity/14compatibility.jar
Ext Folders /Library/Java/Extensions:/System/Library/Java/Extensions:/System/Library/Framew orks/JavaVM.framework/Versions/1.5.0/Home/lib/ext
Java version 1.5.0_07
book:~/documents/programmation/grundkurs/band1/kap3 kurthahn$


(What a hassle, I know, thanks for any help....)

Kurt

May 6, 2007 9:27 AM in response to kurt hahn1

Hi

I'm assuming with these instructions that your Java source files all belong to a library called Prog1Tools. In other words, the library files all begin with "package Prog1Tools;"

The instructions look complicated but just follow them step by step and you should be ok:

Start Xcode
File > New Project > Java Tool Project
Project Name: MyLibrary
Project Directory: ~/MyLibrary
Click on Finish

Go to the Finder and navigate to the folder MyLibrary
Create a new folder within MyLibrary called Prog1Tools

Go back to the Xcode project window and highlight MyLibrary on the left hand side, near the top of the list.

Project > Edit Active Target 'MyLibrary'
A new window opens
Settings > Simple View > Java Compiler Settings
Target VM Version: set to 1.5
Source Version: set to 1.5
Click on red dot to close this window

File > New File... > Pure Java > Java Class
Change the file name to the name of your first library class. Ensure it still ends with .java
IMPORTANT: Change the Location to ~/MyLibrary/Prog1Tools
Click on Finish
An editor window opens. Remove all the text from the editor window and replace it by the Java source of your library class.

IMPORTANT: Ensure that the first line in the file is package Prog1Tools;

Repeat the same process for any other classes in the library.

Return to the main Xcode project window

Build > Build
This should not return any compilation errors. If there are any then you need to fix them.

Go to the Finder
Navigate to MyLibrary/build/Debug where you'll find MyLibrary.jar
Drag MyLibrary.jar to /Library/Java/Extensions

Close down Xcode

Good luck

Bob

May 17, 2007 4:02 PM in response to Bob Lang1

Thanks for your help so far, but it looks as if I'll need some more. I was reading through the jar-section in the Java help, and if I understood it right, building a jar file wouldn't be possible (or useful) in my case, since the classes I want to use are not dependent on each other, they all serve different purposes and have nothing to do with each other. Also, I have to be able to work with classpathes. So, here's what I have been able to do so far:


I'm trying to compile and execute the file Eingabe.java, which imports the package Prog1Tools, located in /Users/kurthahn/Library/Java, which contains IOTools):


book:~/documents/programmation/grundkurs/band1/kap3 kurthahn$ javac Eingabe.java
Eingabe.java:9: package Prog1Tools does not exist

but:

book:~/documents/programmation/grundkurs/band1/kap3 kurthahn$ javac -classpath "/Users/kurthahn/Library/Java" Eingabe.java
book:~/documents/programmation/grundkurs/band1/kap3 kurthahn$

Unfortunately, I didn't succed so far in setting this class permanently, could anyone give me the correct "set"-syntax?
Also, the file I just compiled doesn't execute correctly:
book:~/documents/programmation/grundkurs/band1/kap3 kurthahn$ java Eingabe
Zahl (int) eingeben: (Bestaetigen mit <Enter>)
Exception in thread "main" java.lang.NoClassDefFoundError: Prog1Tools/IOTools
at Eingabe.main(Eingabe.java:19)

The same code works if I move Prog1Tools into the same directory where Eingabe.java is located (of course, in that case I don't have to add the classpath to the javac-command).

Any more suggestions?

Kurt

May 20, 2007 8:30 AM in response to Bob Lang1

Yes, I tried to do that, but I got a few (4) compilation errors. They're supposed to be working, but I think they depend on each other in some ways, and one if them is called GameApplets, maybe Applets can't be build into jar-files?

It's still strange,all those problems, could they be caused my Java installation? I didn't install anything manually, everything's built in, so I don't know if that's a possibility?

May 20, 2007 9:06 AM in response to kurt hahn1

This is unlikely to be a Java installation problem.

I need to see the compilation errors to go any further. That means that I need to see the "build transcript" from the build phase. This isn't shown by default but you can turn it on by doing a build, then going to the build window and clicking on the third icon; it looks like a three rows of dotted lines and is situated between a yellow triange with an exclamation mark and a solid grey triangle.

I really need to see the transcript output by the compiler including the source file name, the line in the source file that caused the error and the error message itself.

Bob

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Java : add package /set classpath

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.