Platform: IntelliJ IDEA Community Edition 2019.2.1
SDK: jdk-12.0.2
OS: Windows 10
Some background:
I am very new to programming, only having taken one CS course so far (Introduction to Object-Oriented Programming), and I am just now starting my second (Advanced Programming and Data Structures).
In that first CS course, I was only ever required to work within the IDE (IntelliJ IDEA), and did not need to work with the files themselves in File Explorer in order to export them, because the professor set students up to be able to push files to a Git repository from which he could pull and run them in IntelliJ on his own computer. Everything worked fine with all of IntelliJ's default settings.
This semester, the new professor is requiring files to be compressed to a zip file and uploaded to a Canvas assignment page, so he can download them from Canvas, extract them from the zip file, and compile and run them from his computer's command prompt. Clearly I will need to test that my code runs correctly from the command prompt if that is the way my professor checks my work.
The problem:
Without changing any of the default settings or structure provided by IntelliJ, the code I write for assignments does not run in the command prompt, even though it runs successfully in IntelliJ's console.
The desired behavior:
The code runs successfully in both the IDE's console and in the command prompt.
Default behavior details:
In IntelliJ, when I right click on the text of my assignment's client class then click on "Run Client.main()", the class runs successfully and the console displays the expected output.
In the command prompt, when I navigate to the directory where the .java files I wrote for my assignment are kept (
cd C:\Users\Nathan\IdeaProjects\CS245\src\personal\hello
), compile the .java files in that folder (javac *.java
), and attempt to run the client class (java Client
), I receive this error message:
Error: Could not find or load main class Client
Caused by: java.lang.NoClassDefFoundError: personal/hello/Client (wrong name: Client)
A workaround:
An action I have identified that produces the desired behavior is removing or commenting out the "package" statements automatically added by IntelliJ at the top of each class. However, this creates new problems that will be discussed soon.
New behavior details:
In IntelliJ, when I right click on the text of my assignment's client class then click on "Run Client.main()", the class runs successfully and the console displays the expected output.
In the command prompt, when I navigate to the directory where the .java files I wrote for my assignment are kept, compile the .java files in that folder, and attempt to run the client class, the class runs successfully and the console displays the expected output.
Problems caused by workaround:
- Deleting the package statements from every class I write before I can test it in the command prompt and upload it for my professor is tedious.
- If there are classes in the same folder that use objects and methods from one another, or that extend other classes and override their superclass's methods, IntelliJ's inspection feature changes the text color of these objects and methods to red, and hovering the cursor over them gives the message
Cannot resolve symbol 'ObjectName'
orCannot resolve method 'methodName'
. Even so, the client class still compiles and runs in both IntelliJ and the command prompt. - If two classes (both lacking package statements) not in the same folder have the same name (e.g. Client, or Main), the class will not run in IntelliJ, but will run in the command prompt. (I understand this may have something to do with both classes being in the "default package," which is a concept I admittedly do not fully understand, especially considering the class still runs with no issues in the command prompt.) The error message produced in IntelliJ is:
java: duplicate class: Client
New desired behavior:
- The code continues to run successfully in both the IDE's console and in the command prompt.
- I do not have to remove all the package statements when I am ready to test my code in the command prompt or upload the files to my professor.
- IntelliJ's inspection does not attach error messages to objects and methods that are shared by multiple classes from a single folder.
- Having multiple classes of the same name that are not in the same folder does not create an error.
Reproducible example:
Here are three very simple classes that reproduce the original problem when the package statement is uncommented, and reproduce the new problems when commented out.
// Replace this with your package's name and uncomment to reproduce original problem.
//package [yourPackageName];
public class HelloSayer {
public String getPhrase() {
return "Hello";
}
}
// Replace this with your package's name and uncomment to reproduce original problem.
//package [yourPackageName];
public class PlanetWhisperer extends HelloSayer {
public String getPhrase() {
return super.getPhrase() + " World";
}
}
// Replace this with your package's name and uncomment to reproduce original problem.
//package [yourPackageName];
public class Client {
public static HelloSayer greeter = new HelloSayer();
public static PlanetWhisperer worldTalker = new PlanetWhisperer();
public static void main(String[] args) {
System.out.println("greeter says: " + greeter.getPhrase() + "!");
System.out.println("worldTalker says: " + worldTalker.getPhrase() + "!");
}
}
How can I achieve the new desired behavior?