开发者

system.out.print doesn't print any result when compiled with javac?

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


  1. open your cmd
  2. 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)
  3. move to the directory where the *.class file exists (make sure its really there :) )
  4. 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.


  1. Make sure you have traversed to till the directory where your file exists.
  2. Then compile the file as javac .java. Check if class file has been generated.
  3. Once it generates, run it as java MyFirstClass
  4. It should execute the program.
  5. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜