Solved

How do I run my java program

Posted on 2012-04-07
9
359 Views
Last Modified: 2012-04-12
I created a java program with Eclipse.  I tried running my program from command prompt (e.g. myprogram.java)

C:\myjavaprograms> java myprogram

but it gives the below error (running javac does not throw an error)

Exception in thread "main" java.lang.NoClassDefFoundError: myprogram (wrong na
me: ceasarcipherprogram/myprogram)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
0
Comment
Question by:CipherIS
9 Comments
 
LVL 3

Expert Comment

by:fjocke
ID: 37820253
From what i can tell , you get No definition for a requested class error, which would imply that you havn't matched the class name of your project to name of your source file.

Name the file for instance mytest.java and then also name the class to mytest.
0
 
LVL 1

Author Comment

by:CipherIS
ID: 37820256
Here is an example of my code.

package ceasarcipherprogram;

import java.util.Scanner;

public class myprogram{
	
	public static void main(String[] args){
             //Code Here
         }
}

Open in new window

0
 
LVL 3

Expert Comment

by:fjocke
ID: 37820275
You need to export your project as jar before you can run it with java.exe.

If you run eclipse, simply just CTRL-F11 to run the code.

Add System.out.println("Hello"); in your main function and you'll see it works :)
0
 
LVL 1

Author Comment

by:CipherIS
ID: 37820479
It runs in eclipse.  I do have more code i just gave a sample.  I was trying to figure out how to run it with command prompt.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:fjocke
ID: 37820725
The simplest way would be to export you code into a .jar file :)

Else you can follow this guide to achieve what you want:

http://www.javaworkspace.com/setClasspath.do
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 37821012
You are not executing the command line fromthe correct path . Eclipse would have created the required class files in bin folder of your project .So, cd to that path and then execute the commandline .
0
 
LVL 2

Expert Comment

by:Elbassel
ID: 37821174
This link should help you:
How to execute java program in CMD in windows?
The main class(that has main method) must be public and the name of the file must be the name of the class.

Best wishes
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 37821367
Try

C:\myjavaprograms> javac -d .  myprogram.java

C:\myjavaprograms>java ceasarcipherprogram.myprogram

Open in new window

0
 
LVL 14

Expert Comment

by:Geisrud
ID: 37823151
Have you installed the java Development Kit (JDK) on your computer so you can compile and run java programs?  Or are you just worked from Eclipse?

You may want to look around this page if you need the JDK
http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html#javasejdk
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now