2

I'm trying to run a simple java test code. I'm getting a "can not find or load main class file "(something like that)

The tutorial I'm following uses this command

-> javac name.java (javac doesn't work, using ->java ..)
-> dir (shows the classname as a file)
 > java classname
 > outputs "hello world"

I can't seem to get past the ->java running.java

class apples //everything begins with a class - need this to do anything
{
    public static void main(String args[])//method
    {
        System.out.println("Hello World");
    }   
}
1
  • You should convers your *.java file into *.class file with javac. It is called "compiling". Commented Oct 5, 2012 at 8:22

4 Answers 4

1

There could be a few problems here. Check the following:

  1. Are you saving the file as the name of the class plus .java, e.g. apples.java
  2. When you execute it, are you typing the name of the class or the name of the class file? you should be typing java apples, not java apples.class or java apples.java.

EDIT:

I Noticed you haven't compiled the program using javac, which makes the progrm unrunnable by java [program_name]. You need to run javac [java_sourcde_file_name] to generate a .class file. If javac doesn't work, maybe:

  1. You don't have the JDK (Java Development Kit) installed and should download it from Oracle
  2. javac is not in your PATH - unlikely but possible - see http://www.apl.jhu.edu/~hall/java/beginner/settingup.html.
  3. javac runs properly but your program doesn't compile properly. this seems unlikely given the program you posted looks fine.
Sign up to request clarification or add additional context in comments.

10 Comments

Nope my java file is names "running".java my class is "apples". 1] I am navigating to "running".java location dir d:\java (lists "running") and then i cant get any further than that. I've tried javac running (error:javac not recognized command). java running(error: can not find or load main class running) so then I try java apples(this is the main class) and also has can not find/load error.
your problem is that you need to install javac. look at the second part of my answer.
I installed the java SE from java.sun.com. That thought occured to me so I downloaded java jdk and installed again (twice) but with both installations it said I already had the software installed etc but I let it carry through anyway. I've already changed my Path variable and added the D:\Program Files (x86)\Java\jre7\bin.
go to the command line and type dir "D:\Program Files (x86)\Java\jre7\bin". is javac.exe listed?
Actually looking at the site you linked it may have something to do with the path I am pointing it to. Ill go through the turial and see how that turns out.
|
1
class Apples //- need this to do anything
{
    public static void main(String args[])//everything begins with a method
    {
        System.out.println("Hello World");
    }   
}

Make sure you are in current directory of java file

compile as

javac Apples.java

Run as

java Apples

Before working in it , should need to know the coding convention it would be better to work java

Comments

0

Well. All you have to do in Jaca is navigate you where your class files are stored and then use java "class name". You DO NOT need to put .java or .class. Just the name.

3 Comments

Could not find or load mian class apples. This is what I've typed in -> dir d:\java (lists my running.java file) -> java running(could not find or load...etc) -> java apples (class name) also has find/load error)
Oh. I see. You haven't compiled the files yet. You have to use javac.
Continue following Jake's answer as he is explaining possible reasons why javac is not working. I'll comment there if I find anything interesting.
0

change your class name from apples to Apples (naming convention for java class names): http://en.wikipedia.org/wiki/Naming_convention_%28programming%29#Java

also recommend you to change the file name accordingly...

then recompile and run it

javac Apples.java

java Apples

cheers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.