How to compile java package structures using javac

前端 未结 5 609
逝去的感伤
逝去的感伤 2020-11-29 06:20

I am trying to compile (from the command line) a java package that imports another package of my own. I was following a tutorial online but it seems that I get an error when

相关标签:
5条回答
  • 2020-11-29 06:28

    (1)first compile the code

    javac -d importpackage.subpackage.HelloWorld

    (2) and then compile the CallPackage.java

    javac CallPackage.java

    0 讨论(0)
  • 2020-11-29 06:32

    Are you sure importpackage/subpackage is in your classpath?

    -cp path or -classpath path

    Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

    If the -sourcepath option is not specified, the user class path is also searched for source files.

    If the -processorpath option is not specified, the class path is also searched for annotation processors.

    http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html

    0 讨论(0)
  • 2020-11-29 06:34

    The issue was that the class path needs to be set for each command (javac and java):

    Attempted Steps

    1. instead of going to subpackage, compile HelloWorld.java from the top_level:

      $javac -cp . importpackage/subpackage/HelloWorld.java

    2. compile CallPackage.java in the same way:

      $javac -cp . CallPackage.java

    3. run the file using the class path also:

      $java -cp . CallPackage

    NOTE: running "$java CallPackage" will give an error "Error: Could not find or load main class CallPackage"

    In summary, during each step, the class path must be specified. It worked after running it as such.

    0 讨论(0)
  • 2020-11-29 06:34

    delete your package folder (after pasting you code to some other folder) and then locate to the folder in cmd where you current code is and try javac -d . Helloworld.java (this will create the Helloworldclass and subpackage as well) and try same for you mainfunction code ie Callpackage.java after compiling to run the code try java Callpackage

    0 讨论(0)
  • 2020-11-29 06:39

    Same situation to me. And I came to take over it by compiling classes at the same time.
    For example, here is my project:

    + beerV1
       -> classes
       -> src
             -> com
                  -> example
                       -> model
                            -> BeerExpert.java
                       -> web
                            -> BeerSelect.java
    


    BeerExpert.java:

    package com.example.model;
    import ...
    
    public class BeerExpert{
        ...
    }
    


    BeerSelect.java:

    package com.example.web;
    import com.example.model.*;
    import ...
    
    public class BeerSelect {
          ...
    }
    


    As you can see: BeerSelect.java is trying to import classes in com.example.model package.
    At the first time, I compiled BeerExert.java first by command:

    --> javac -d classes src/com/example/model/BeerExpert.java

    Then:
    --> javac -d classes src/com/example/web/BeerSelect.java

    And the result was:
    -->... error: package com.example.model does not exist

    So, I knew that compiling multiple classes separately will not work in this case.


    After suffering on google, I found this very simple way to solve the problem:
    Just compile all at once:

    --> javac -d classes src/com/example/model/BeerExpert.java src/com/example/web/BeerSelect.java 
    


    Finally, here is what I got:

     + beerV1
               -> classes
                     -> com
                          -> example
                               -> model
                                    -> BeerExpert.class
                               -> web
                                    -> BeerSelect.class
               -> src
                     -> com
                          -> example
                               -> model
                                    -> BeerExpert.java
                               -> web
                                    -> BeerSelect.java
    

    Hope that helps.

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