Results 1 to 8 of 8
- 03-20-2011, 01:18 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
My first java program could not be executed..help
Hello,
import java.io.*;
public class firstjava1
{
public static void main(String args[])
{
System.out.println("hi");
}
}
C:\>javac firstjava1.java
C:\>java firstjava1
Exception in thread "main" java.lang.NoClassDefFoundError: firstjava1
Caused by: java.lang.ClassNotFoundException: firstjava1
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: firstjava1. Program will exit.
Thanks in advance..
I have coded the above java program and it creates the java class file when compiled but it don't allows me to execute the file..
My Path is set as : C:\Java\jdk1.6.0_24\bin
Can anyone help me out where's the mistake...??
- 03-20-2011, 01:31 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
go to the directory it's in and compile it with the classpath flag.
Also, when creating classes you want to follow correct practice(capitalize first letter of each wordJava Code:java -cp . firstjava1
Also, it may be helpful to save your java code to a new directory, for instance I tend to have an easier time having a directory located at c:\javacode, and in this directory I store all my java code, and set the path to point to this directory.Java Code:public class FirstJava1
Also, consider adding . to your classpath, this will allow it to use your current directory so you can just cd to the directory and compile and run easily.
Let me know if this works, if not, I will try my best to help you out more.
- 03-20-2011, 02:45 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Hello,
thanks for your reply and help..
i tried out as you suggested.
from your suggestions i tried all in the different ways as you mentioned..
It could give the output only by using -cp
java -cp . FirstJava1
If i tried it
java FirstJava1
it gives me the error why is it so ??
For what -cp is used to execute the program ??
- 03-20-2011, 03:23 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
thanks....
so i have to change the path that means...anyways
if i want to run in simple java filename then i can't execute the file or what ??
i have to set my path within C:\program files\java only?? please tell me the path to execute the file..Thanks for your help..It cleared my questions now just help me to set the path..Last edited by miaaa00; 03-20-2011 at 03:27 PM.
- 03-20-2011, 05:36 PM #5
Member
- Join Date
- Mar 2011
- Location
- chennai
- Posts
- 55
- Rep Power
- 0
to set the path
right click on MyComputer->choose properites->choose tab Environmental variables-> go to system variables section check for PATH variable is there. if available select that one and click edit
in the value section path is already set for other ones so go to the end of the value like c:/p files/.../...;c:/prog files/..../....;.;
just go end remove ;.; and paste the java path without effecting the previous like this:
c:/p files/.../...;c:/prog files/..../....;c:/program files/java/jdk1.6/bin;.;
then click ok button.
we can differentiate the path by ; (semicolon), this is the delimiter for several paths
- 03-20-2011, 08:01 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
As stated above, using ;(semicolons) lets you add multiple classpaths. For example, it may be useful to have your classpath set up with the bin(like you already have), followed by these two commands
This will allow you to compile and run files in the a directory called javacode, and the current directory.Java Code:theBinFolder;c:\javacode;.
The reason -cp . works is because it's a special flag which tells the jvm to use the following classpath when executing the program. The command . means current directory, so using this flag with this makes it work.
If you have a file in the directory
You can also compile and run withJava Code:c:\javacode
Here is more reading if you are up to itJava Code:javac -cp c:\javacode FileName.java java -cp c:\javacode FileName
java - the Java application launcher
javac - Java programming language compiler
Finally, the suggestion on using
is more of a do this, not that.Java Code:FirstJava1
When you write programs you should make your file and class name have a capitalized first letter(this is not needed, but generally accepted)
- 03-21-2011, 07:41 AM #7
Member
- Join Date
- Mar 2011
- Location
- chennai
- Posts
- 55
- Rep Power
- 0
you said that to run java -cp . filename to execute.
if we set the like theBinFolder;c:\javacode;. can we directly execute the program like this?
c:\javacode> java FirstJava
or else can we place the -cp . for every time we run
- 03-21-2011, 08:07 AM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
instruction apparently not executed
By rippon in forum AWT / SwingReplies: 4Last Post: 11-30-2010, 01:30 AM -
Finally does not get executed
By rahulkumar in forum New To JavaReplies: 8Last Post: 04-06-2010, 05:30 PM -
How to make the actionPerformed be executed till the next Button click
By Laythe in forum AWT / SwingReplies: 9Last Post: 11-01-2009, 05:45 AM -
Will finally get executed...
By AlmostAGuru in forum New To JavaReplies: 1Last Post: 08-10-2009, 07:12 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks