1

I'm doing my program. I'm using Sublime Text and for compiling basic Windows CMD. I have folder, where I have every file (folde todolist) In this folder I have package. In this package I have class (Gui.java(packgae (folder) gui_pckg)). Code in this class looks like:

package  gui_pckg;

import javax.swing.; import java.awt.;

public class Gui {

public JFrame frame = new JFrame("To Do List");


public Gui() {
    frame.setSize(500, 800);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

}

And it's compiled well.

But, when I'm trying to compile Main.java, it throws errors. Code in Main.java:

import gui_pckg.Gui;

public class Main {

public static void main(String[] args) {
    Gui gui = new Gui();
}

}

For compiling I'm typing basic compile command into cmd.

Commands in cmd

Please help.

  • You have to tell the compiler where to find referenced classes. See https://stackoverflow.com/questions/4764768/java-how-can-i-compile-an-entire-directory-structure-of-code – JayC667 Jan 02 '21 at 14:33

1 Answers1

1

Use an IDE. Trying to compile on the command line when you have an intricate packaging stucture quickly becomes too troublesome for the payoff.

Without an IDE, you can expect to tweak all packages for files you move and tweak all package names for package-files when you rename the package. Not to mention, even if you make a bash script for example, to compile your java program, this requires manual tweaking every new file for example. TLDR: Just use an IDE.

If you absolutely insist on continuing without one (not recommended at all), to avoid complexity, all of your java classes should be in the same package (directory), AKA, devoid of packaging. Java sees all classes in the same dir. You can import them without packaging.

When you start a structure like this:

|-bin
|-src
   |-util
   |   |-Util.java
   |-Main.java

Then you need to start packaging because not all files exist in the same directory anymore. Avoid this or you'll have to setup your classpath, binary, and source folders manually.

In bash, this is something like:

cd src; javac -cp ".:util" -d "../bin" ./Main.java ./util/Util.java

Then to run, you'd have the following:

java Main

The cd to src is actually insanely important because if you do not compile being in the top root directory, the packages won't make sense to javac.

simbleau
  • 76
  • 1
  • 4