r/learnjava • u/BoyTitan • Jun 18 '21
Relearning java after years of not programming after college. I can compile fine in my IDE but not Command Prompt
So I am using a recommended tutorial by John Purcell and noticed right away he is using Eclipse which is to resource heavy for me so I am using NetBeans, then I also noticed what should be in the introduction of using a command prompt and notepad+ like my professor thought is missing. So I had to reteach myself how to use the command prompt for java. I do all that get the could not find or load main class error. I go set the paths in environmental variables. For some reason only 1 file will properly compile in command prompt after that. I can copy and paste the code into a new project properly rename it then when I try to type in java and " insert program name" after compiling it in javac it won't load. I could see if nothing I program worked in the command prompt it would be a simple path issue but 1 program thats in the same exact folder as the others works. I am five minutes from just being a IDE noob at this point because this is 2 days of Could not find or load main class issues now. The IDE finds it fine this didn't happen when I used to program years ago I just can not make sense of this. !!!! Update !!!!! I figured out the problem really stupid I didn't notice been making projects with ant and I don't think thats what I was using 6 years ago. It adds a line that says package followed by the name of my class/file name. I deleted the line that said package and it works in command prompt. I should have from the beginning just opened up the java file that worked in notepad and looked for differences.
9
u/EmbarrassedTaste7745 Jun 18 '21
Could not find main class simply means there is no main method in your program or even if it is there, maybe the class is not public or maybe the name of the class is not the same as the file name..for java to work, the public class with the main method should have the same name as the filename..
2
2
1
u/BoyTitan Jun 19 '21
!!!! Update !!!!! I figured out the problem really stupid I didn't notice been making projects with ant and I don't think thats what I was using 6 years ago. It adds a line before everything that says package followed by the name of my class/file name. I deleted the line that said package and it works in command prompt. I should have from the beginning just opened up the java file that worked in notepad and looked for differences.
4
u/khooke Jun 18 '21
We all live in our IDEs and all forget how to do this. Don't worry about it, we all go through this. The other answers about checking your package name and including it as the dir structure passed to the java.exe is probably what you're running into.
1
Jun 18 '21
Whats the reason for learning command prompt asides from just general interest? if i can use an IDE why make it harder on myself- makes me think of the "one day u wont have a calculator" argument
1
u/BoyTitan Jun 19 '21
To simply know I can still do it, fixed the issue and determined I can still do it. Feels good.
3
u/kelzerbob Jun 18 '21
It would be helpful if you were to post the source code for your class. In general, when seeking Java help you should also mention what version of java you're using.
As others have said, your class needs a main method. It has to be declared as "public static void" with argument list of "String[] args" (well, technically, you can change "args" to any name you want, e.g., "String[] parms"). If it doesn't conform to that standard, the compiler won't find it, and you'll get the error you're seeing. If you can run the program from within NetBeans, then you should be ok to compile and run at the command line.
Here is an example:
package com.example.myproject;
public class TestClass {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Your source file should be named "TestClass.java" and it should be in subdirectory com\example\myproject (for Windows, com/example/myproject for Mac/Linux).
You can compile from the parent of the "com" subdirectory:
javac com\example\myproject\TestClass.java
Or you can use "cd com\example\myproject" to go into the directory containing your Java file, and then run:
javac TestClass.java
But, to run it, you should go back to the parent directory of "com", i.e., "cd ..\..\..".
Again, you should *not* be running it from inside the com\example\myproject subdirectory.
From the parent directory of "com", i.e., if you use the "dir" command you see the "com" subdirectory, run:
java com.example.myproject.TestClass
You should see:
Hello world
Note that the javac command, which compiles your Java source code into a ".class" file, needs backslashes (or slashes on Mac/Linux) to indicate the directory structure leading to the .java file, but the java command, which runs your program, uses dots, just like in the package name that you declared inside your file.
1
u/Niyaz_Ahamed_0109 Jun 19 '21
This is old technique but it will workout with all the aspects properly.
Without using IDE, we can able to run the Java programming by using command prompt, before that you must ensure the JDK and JRE not is Local Disk C: place that both folders in some other Local Disk. The reason behind it, some of the System file can't able to modify in Local Disk C:.
Set the path in command prompt to that specific Local disk where you place the both folders. Then start compiling the Java programming. You may write program in Notepad++ and compile that Java file in Command prompt is possible. Want to learn more Java Programming you can contact Java Training institutes and there are lot of options in Online free websites like Github, Javatutorial, and etc.
-14
Jun 18 '21
[removed] — view removed comment
21
u/BonfireinRageValley Jun 18 '21
Omg why are people such giant assholes in a learning sub. Who cares how basic it is, the sub is called /r/learnjava, not /r/mysuperioritycomplexisshowing
13
u/NautiHooker Jun 18 '21
If its that easy then why dont you just help OP instead of mocking them?
This is not how we expect help to be given on this subreddit.
Your comment was removed.
•
u/AutoModerator Jun 18 '21
It seems that you are looking for resources for learning Java.
In our sidebar ("About" on mobile), we have a section "Free Tutorials" where we list the most commonly recommended courses.
To make it easier for you, the recommendations are posted right here:
MOOC Object Oriented Programming with Java from the University of Helsinki
Java for Complete Beginners
accompanying site CaveOfProgramming
Derek Banas' Java Playlist
accompanying site NewThinkTank
Hyperskill is a fairly new resource from Jetbrains (the maker of IntelliJ)
Also, don't forget to look at:
The official Documentation
The official Java Tutorials
Our community resources thread
If you are looking for learning resources for Data Structures and Algorithms, look into:
"Algorithms" by Robert Sedgewick and Kevin Wayne - Princeton University
Coursera course:
Part I
Part II
Coursebook
Algorithms 4th Edition
Your post remains visible. There is nothing you need to do.
I am a bot and this message was triggered by keywords like "learn", "learning", "course" in the title of your post.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.