Solved

How do I run my java program

Posted on 2012-04-07
9
352 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

759 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

17 Experts available now in Live!

Get 1:1 Help Now