Solved

How do I run my java program

Posted on 2012-04-07
9
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
throw exception 21 79
Android development question 2 110
mysql jsp example issue 32 96
CSV file parsing thru Java 13 54
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

752 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