5 Replies - 198 Views - Last Post: 13 September 2012 - 09:02 AM Rate Topic: -----

#1 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Why isn't my program working through my command line (log in Putty

Posted 13 September 2012 - 08:30 AM

This is the code. It runs fine in the netbeans but doesnt in the command line.
Below is my code and below that is the error I get in my command line when tried to run it.

import java.util.Random;

public class GenerateData {

    public static void main(String[] args) {
        Random numGen = new Random();

        int range = 10;
        int noi = 10;
        for (int i=0; i<noi;i++ ) {
            int NewGenNum = numGen.nextInt(range);
            System.out.println(NewGenNum);
        }

    }
}




myname> java GenerateData.java
Exception in thread "main" java.lang.NoClassDefFoundError: GenerateData/java
Caused by: java.lang.ClassNotFoundException: GenerateData.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: GenerateData.java. Program will exit.


Is This A Good Question/Topic? 0
  • +

Replies To: Why isn't my program working through my command line (log in Putty

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,200
  • Joined: 05-April 11

Re: Why isn't my program working through my command line (log in Putty

Posted 13 September 2012 - 08:49 AM

you should write java GenerateData and not java GenerateData.java

Also the compiled classes are having a .class extension not .java ;)

Aaaand did you compile your classes using javac ?
Was This Post Helpful? 0
  • +
  • -

#3 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Re: Why isn't my program working through my command line (log in Putty

Posted 13 September 2012 - 08:53 AM

View PostCasiOo, on 13 September 2012 - 10:49 AM, said:

you should write java GenerateData and not java GenerateData.java

Also the compiled classes are having a .class extension not .java ;)

Aaaand did you compile your classes using javac ?


Thanks for the response.

I tried doing java GenerateData too. No luck!
I checked my file it says .java as an extension.
Do not know what exactly javac is, but i used NetBeans to code it if that helps. :/
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,200
  • Joined: 05-April 11

Re: Why isn't my program working through my command line (log in Putty

Posted 13 September 2012 - 08:56 AM

You have to compile your code before you can run it

javac GenerateData.java will compile your code
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5421
  • View blog
  • Posts: 8,707
  • Joined: 19-March 11

Re: Why isn't my program working through my command line (log in Putty

Posted 13 September 2012 - 08:58 AM

javac is the compiler. You want to run that first:

>> javac GenerateData.java

If that succeeds - that is, if you see no output, and it just gives you a prompt - then you run the program by invoking a java virtual machine and giving your GenerateData class as the entry point:

>> java GenerateData


Notice that the first command operates on a source file, "GenerateData.java", which contains text. The second operates on a class, GenerateData, which java will find by looking in a file. That is, you don't want

>> java GenerateData.class

This will fail!
Was This Post Helpful? 1
  • +
  • -

#6 deprosun  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 16-November 10

Re: Why isn't my program working through my command line (log in Putty

Posted 13 September 2012 - 09:02 AM

Let me try..

WORKEDD!!! yess thank you so much all.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1