How to compile packages in java?

前端 未结 4 1834
深忆病人
深忆病人 2020-12-01 01:55

I have written a simple package program:


  //A simple package

       package MyPack


       class Balance
       {
        String name;
        doubl         


        
相关标签:
4条回答
  • 2020-12-01 02:02

    Try to create the folder named MyPack and move .class files in it or use following command line to compile the java code and it moves the .class files into MyPack folder.

    javac YourFileName.java -d .
    

    and run using,

    java MyPack.AccountBalance
    

    Have a look at javac tool

    From the tool doc : By default, the compiler puts each class file in the same directory as its source file. You can specify a separate destination directory with -d (see Options, below).

    and package tutorial.

    0 讨论(0)
  • 2020-12-01 02:06

    Best is to compile and run the classes from outside the packages :

    First you compile with javac :

    $javac MyPack/AccountBalance.java
    

    this will create a new file in the MyPack folder called AccountBalance.class

    then you can run it :

    $java MyPack.AccountBalance
    

    By the way: it is discouraged to have package names start with a capital.

    0 讨论(0)
  • 2020-12-01 02:08

    When you are trying to compile the java class, use the '-d' option (destination) to specify where the .class files should be located.

    javac -d "classes" AccountBalance.java
    

    and when you run your program, make sure that same folder is included in your class path:

    java -classpath "classes" MyPack.AccountBalance
    
    0 讨论(0)
  • 2020-12-01 02:22

    If you're frequently compiling and running via javac and java commands then use

    javac MyJavaClass.java && java MyJavaClass
    

    This'll compile the the class then run the class you just compiled.

    Note: Replace && with ; if using Windows powershell.

    0 讨论(0)
提交回复
热议问题