No main method can be found [FIXED]

Hello everyone.
First of all, I think this post is only for those who are using no IDEs, only the Java Development Kit and cmd.
Ok. I’m having troubles with executing my projects. Yesterday, all worked well. In cmd, when in my project’s repertory, when I wrote

java <class containing the main method>

, all worked fine and the project ran. But… Now, it won’t open the project (even those I had done before) using the command, saying I have no main method that can be found. It seems I have to do

java -cp "%cd%" <class containing the main method>

:-
One change I have noticed is that the

echo %CLASSPATH%

command returns “null” :o Before, I’m sure it was something more like “%classpath%;c:\Users etc”. Moreover, no matter what I set it to (with the

set %CLASSPATH%

command) its value won’t change :persecutioncomplex:
I’ve tried installing Java again and it changed nothing. Could some charitable god help me out of this now ? :3 Thanks a LOT !
J0

Can you show us a complete example from start to finish, including the actual code you’re trying to run (the smaller the better) as well as the text copied directly from the command line?

There is very little information with which to help you. Could you also show the directory structure?

Also as far as I know, there is no CLASSPATH global environment variable.

Sure. Here’s an extremely small code (don’t think we can actually do smaller) :

class Main{
    public static void main(String [] args){
        System.out.println("Java works!");}}

As I said, cmd won’t allow me to run it (not finding the main method)… I won’t copy the text from cmd as my computer is set to French, but this is approximately what it’s saying : “Error: it is impossible to find or load the main method.”
Thanks :wink:

We need to see exactly what you’re typing into the command line, as well as the directory structure where you’re executing those commands.

Well, as you want. I have my code here :

C:\Users\Me\Desktop\Project\Code

And my main class is called Main. So what I type is:

cd Users\Me\Desktop\Project\Code
javac Main.java
java Main

Maybe you has a package defined in Main?

java yourpackage.Main

Nope, no packages…

And “Main.class” is indeed in the directory? (It’s been eons since I’ve used javac).

Sure it is ;D

Set JAVA_HOME to your variable path. Do a google search.

I’ll try and do that ! Thanks a lot ! 8)
J0

Thank you SO MUCH -
I dunno why it used to work, then stopped working, but never mind… IT WORKS ! ::slight_smile:
J0

That fix really doesn’t make any sense. If you’re just using java and javac, your JAVA_HOME variable is never read. The only thing that matters is your PATH, and if java and javac can be found, your PATH is fine.

I would bet that you were simply in the wrong directory or something, and fixed yourself without realizing it.

I don’t know either how this fixed it, but it actually DID, so sorry, but it seems you’re wrong. I’m absolutely SURE I didn’t change directory. :cranky:
Never mind though. It works, so that is enough for me for now :stuck_out_tongue:

You are right cmd doesn’t read that but after all these years… sometimes redirection with punch line works meaning when you search to set up java_home in google and find articles on it, you will usually be also asked to update your path to %JAVA_HOME%\bin which gets the job done. I admit though I was a bit lazy when answered.

[quote=“J01,post:15,topic:49539”]
I issue you a challenge: change your JAVA_HOME back to whatever it was before. I bet it still works.

Anyway, if it didn’t change anything with my problem, could you explain me how I now have it all right now ? ???
J0

[quote=“J01,post:18,topic:49539”]
What do you think JAVA_HOME is used for?

JAVA_HOME is used by third party programs that need to know where Java is but don’t want to rely on your PATH. You can also use JAVA_HOME in your PATH to avoid changing your PATH whenever you upgrade Java.

Both java and javac worked for you, which means that your PATH was working fine. Java doesn’t look at JAVA_HOME at all.

I predict that you exited a window or command line to go change your JAVA_HOME, then reopened it in the correct location. It was not the changing of the JAVA_HOME that fixed your problem, but the reopening of the window or command prompt. If you didn’t exit the command prompt, then it definitely wasn’t changing JAVA_HOME, as those changes are not detected by command prompts that are already open.

I definitely didn’t have the incorrect repertory open in my cmd, I’ve had this problem for about 2 days and tried a few billion times ;D