making a java package in the command line

前端 未结 5 792
别跟我提以往
别跟我提以往 2021-01-05 01:00

Though it\'s probably reccomended one uses and IDE for coding advanced java projects, I personally prefer running almost entirely command-line (using gedit as a text-editor)

相关标签:
5条回答
  • 2021-01-05 01:38

    packages are just directories on the filesystem. so your package: com.mycompany.util corresponds to a directory com/mycompany/util.

    When running and compiling etc your current workding directory should be where that top directory is located.

    To include libraries, include them in your classpath when compiling and running. For example make a Project directory myproject and under that have your java-files and packages under myproject/src/ and libraries that you use under myproject/libs/ Then when your current workding directory is myproject execute java -cp .:libs/*.jar or the same with javac.

    But I suggest you look into using ant or maven.

    0 讨论(0)
  • 2021-01-05 01:39

    // to create a new directory and a subfolder that will be your package

    $ mkdir -p parent/child 
    

    // to move into it :

    $ cd parent/child                    
    

    //to create an empty java file

    $ touch MyClass.java
    

    //to edit current file

    $ nano MyClass.java
    
    package child; 
     public class MyClass { 
    }
    

    PS: The directory structure on your computer is related to the package name. That means when you edit .java file it needs to have a package declaration(your package) otherwise you will have a default package (ex: java.util.*).

    0 讨论(0)
  • 2021-01-05 01:44

    You can get along just fine on the command line by using a packaging tool such as Ant or Maven. Maven is especially handy because it is a higher level tool that already knows how to build various project types: command-line apps, webapps, libraries, etc. It also handles library dependencies by downloading them from repositories.

    0 讨论(0)
  • 2021-01-05 01:49

    There are three parts to it: (1) create directory structure; (2) indicate package in java file; (3) compile it.

    For example, if you want to create package com.mycompany.myproject, then you need to start in the base directory for your project and then:

    (1) create directory com/mycompany/myproject

    (2) create java files in that directory, stating package com.mycompany.myproject in them;

    (3) compile the files, for example, with javac -cp . com/mycompany/myproject/*.java

    You may want to specify a different output directory so as to not mix sources and compiled classes.

    If you need to use external libraries (.jar files) to compile, then you need to use -cp or -classpath command-line parameter to javac tool to specify them, e.g.

    javac -cp .:some_library.jar:lib/another_library.java com/mycompany/myproject/*.java
    

    It may be a good idea to put all external libraries in one place, e.g. lib subdirectory of your main project directory. And, by the way, the above javac command assumes unix-like environment. If you're on Windows, then you'll need to use ; for path separation.

    0 讨论(0)
  • 2021-01-05 01:52

    Java Package is just a directory structure, so a simple way of creating a Package lets say com.organization.test in terminal will be

    mkdir -p com/organization/test
    
    0 讨论(0)
提交回复
热议问题