Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I run my java program

Posted on 2012-04-07
9
Medium Priority
?
382 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses

916 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