Either nobody in the world from the myriad of forums and discussion boards I have been to knows how to do this, (which seems unlikely, as this does not seem a complicated procedure) or none of those people know how to answer a question concisely and in a straightforward way.

I want any answer to be in very simple step-by-step instruction format. I am asking you to treat me like I am stupid, please.

1. I have a file called 'Encryptor.java' that I made.
2. I put 'Encryptor.java' into a directory/folder called 'tools'
3. I have another file called 'encryptorTest.java'
4. 'encryptorTest.java' has a line of code 'import tools.Encryptor;'
5. Later in the file it says, ' Encryptor e1 = new Encryptor()'. This line also gives me an error.

The preceding line gives an error upon compilation. How do I modify this program so that it runs as intended, allowing the programmer to declare a variable of type 'Encryptor' in the file 'encryptorTest' with the name 'e1'?

Along with your answer, please include:
- The directory structure of the files (including sibling files, symbolic links, aliases, invisible file(s), parent directory, 5\five or six ancestor directories, all children directories.
- What the classpath should be, where/when to change it, (in a file with a certain name and/or in a certain location OR as a terminal command such as 'set CLASSPATH'

*ALSO*
Please make sure the method is reproducible, and not something that will only work once, and in one place for reasons including but not limited to: the parent directory being on the default class path, planetary alignment, names that happen to mimic/be similar to names that are set up by default, etc...

Thank You

Recommended Answers

All 13 Replies

Hi, I have a suggestion:
The directory/folder, 'tools' and the file, 'encryptorTest.java' should be in the same directory/folder.
I'd like to know what errors you are getting.

Thanks!
Varsha.

I tried that/currently have that setup. 'tools' is in the directory with 'encryptorTest.java'. Any other ideas/anyone else?

ok, it took a VERY long time, but I finally got it to work! Just add each dir containing compiled class files to classpath! yay :)

Just add each dir containing compiled class files to classpath!

Really you need to put the dir at the head of the package path on the classpath.
If a class is in package mystuff.work, the class file would be in the work folder and the classpath should point to the folder containing the mystuff folder.

Member Avatar for hfx642

The easy way is to complile your application with the ClassPath switch.

javac -cp . encryptorTest.java

But what if the class is in a package and references other classes in that same package?
You need to point the classpath the head of the package. For example if the package path is one deep:
javac -cp ..\.;. YourProgram.java

Member Avatar for hfx642

Isn't that what the -d switch is for?

javac -d . PackageDir\MyClass_1.java
javac -d . PackageDir\MyClass_2.java
javac -cp . MyProgram.java

It seems to work okay for me!
MyProgram calls objects in MyClass_2,
which calls objects in MyClass_1 without any difficulties.

I've never used the -d option.
Is MyProgram in a package? What folder is it in relative to those shown?

Member Avatar for hfx642

No, MyProgram is not IN the package, but calls the package.
import Resources.*;

The called classes also have the statements...
package Resources; // which puts them into the package
import Resources.*; // which allows the called classes to call other classes

I store the sources (*.java) of the called classes in the PackageDir sub-directory.
When they are compiled with the -d switch, they are compiled into the Resources sub-directory (thus, the called package).

What if MyProgram is in the PackageDir package?

You're much better organized than I am. I put everything in the same package in the same directory.

Member Avatar for hfx642

A package is used to put your called subroutines in.
You don't put your main program into your package.
Maybe this will help...

Java_Projects\Project_1\Package_Sources\*.java
Java_Projects\Project_1\Called_Packages\*.class
Java_Projects\Project_1\MyProgram.java (and, once complied, MyProgram.class)

You don't put your main program into your package.

Sorry, there are many apps that are in packages.

Member Avatar for hfx642

Well... If you want to program that way... That's your choice.
I'm just saying that I don't.
And I'm not craping on anyone for doing things their way.
A question was asked, and I just answered what I do.
"Be Happy!"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.