Solved

How do I run my java program

Posted on 2012-04-07
9
362 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 36
Java Timer (static) 9 43
custom annotations 9 38
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 31
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

785 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