im totally newbie to java world and this my first 4 lines of codes and it give some error which i can't resolve
public class MyFirstClass
{
public static void main (String[] args)
{
int x = 3 ;
String name = "Drik";
x = x * 10 ;
System.out.println("x is " + x);
}
}
when i compiled this code with javac its creating the class file but it have output in the screen when i write the command to compile it it give another command code without printing the r开发者_StackOverflow中文版esult
The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into bytecode class files.
javac MyFirstClass.java
To run the class file, actually print the result you want, you have to run the bytecode class with java command like
java MyFirstClass
- open your cmd
- run java -version if you have an output - your java is installed ok. If not - install java correctly (install the jdk, define JAVA_HOME env. variable + define the path environment variable to look at %JAVA_HOME%\bin)
- move to the directory where the *.class file exists (make sure its really there :) )
- run the following command:
java MyFirstClass
This should work
If it doesn't for some reason, try the following instead: java -cp . MyFirstClass
Good luck!
hey may be there is issue with the classPath.. if you are using windows type set in command prompt (export in ubunto) it shows list of pathvariables, check for classPath variable. if its not pointing to the directory where in you have saved your class, use set command to set its path. hope this will work.
- Make sure you have traversed to till the directory where your file exists.
- Then compile the file as javac .java. Check if class file has been generated.
- Once it generates, run it as java MyFirstClass
- It should execute the program.
- Please check the classpath of 'JAVA' in environment variables once.
javac
does not run the file, it will compile your MyFirstClass.java
file to bytecode, creating MyFirstClass.class
file.
Then you can actually run the program by doing:
java MyFirstClass
精彩评论