0

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

4
Contributors
13
Replies
14
Views
6 Years
Discussion Span
Last Post by hfx642
Featured Replies
  • 1

    [QUOTE]You don't put your main program into your package.[/QUOTE] Sorry, there are many apps that are in packages. Read More

0

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.

0

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

0

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 :)

0

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.

0

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

javac -cp . encryptorTest.java

Edited by hfx642: n/a

0

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

0

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.

0

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

0

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).

0

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.

0

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)

1

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

Sorry, there are many apps that are in packages.

0

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!"

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.