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

Dani AI

Generated

Most likely cause: a mismatch between the package statement, the folder layout, and what’s actually on the classpath. The thread already touched this — was right to check folder placement, pointed to the compiler options, and is correct that the classpath must point to the parent (the head) of the package tree. The reproducible workflow below avoids relying on “defaults” and works every time.

Steps (very simple)

  1. Make the class file declare the package that matches its folder. For example, the top of Encryptor.java must be:
    package tools;
    public class Encryptor { public Encryptor() { } /* methods */ }
  2. Put Encryptor.java in a folder literally named tools under a single source root. Keep your test file (encryptorTest.java) in the source root (or in its own package) — don’t mix package files into the default package if you intend to import them.
  3. Compile so .class files are written into a dedicated output folder that mirrors packages, and run with the output folder on the classpath (the classpath must point to the output folder’s parent-of-package, not to the package folder itself).

Example layout (five ancestors shown)

/home/alice/projects/2025/crypto/src/
  tools/
    Encryptor.java
  encryptorTest.java
/home/alice/projects/2025/crypto/bin/    <- compiled .class files go here

Where to put the classpath and what to check

  • For a one-off, pass the classpath on the javac/java command line (don’t rely on an env var). For a persistent change, set CLASSPATH in your shell or system environment to include the bin (output) directory.
  • Quick checks when things fail: ensure package name matches folder name exactly (case matters on Unix), confirm a matching Encryptor.class exists under bin/tools/, and avoid trying to import classes from the default package into a named package.

This explains why adding the compiled directory to the classpath (what did) fixed it — it makes the JVM/ compiler see the package root so tools.Encryptor can be found.

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 Member #647493

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 Member #647493

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 Member #647493

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 Member #647493

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 Member #647493

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.