简体   繁体   English

在命令行中制作一个java包

[英]making a java package in the command line

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).虽然它可能是推荐的一种用途和用于编写高级 Java 项目的 IDE,但我个人更喜欢几乎完全运行命令行(使用 gedit 作为文本编辑器)。 So please don't just tell me "Just use eclipse!"所以请不要只告诉我“只要使用 eclipse!” or something :P或者什么:P

My question is what the method of creating a package in java is by a command.我的问题是在java中通过命令创建包的方法是什么。

I'm not talking about packaging an application that runs in the command line, I'm talking about making a package in the command line.我不是在谈论打包在命令行中运行的应用程序,而是在谈论在命令行中创建一个包。 Am I making a text file?我在制作一个文本文件吗? Am I making a directory?我在做一个目录吗?

Relatedly, how does one link to related libs and natives without use of an IDE?相关地,如何在不使用 IDE 的情况下链接到相关的库和本机?

I know I'm being really awkward here, but I really prefer the control one gets working in command line.我知道我在这里真的很尴尬,但我真的更喜欢在命令行中工作的控件。

There are three parts to it: (1) create directory structure;它分为三个部分:(1)创建目录结构; (2) indicate package in java file; (2)在java文件中指明包; (3) compile it. (3) 编译。

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:例如,如果要创建包com.mycompany.myproject ,则需要从项目的基本目录开始,然后:

(1) create directory com/mycompany/myproject (1) 创建目录com/mycompany/myproject

(2) create java files in that directory, stating package com.mycompany.myproject in them; (2) 在该目录下创建java文件,并在其中声明package com.mycompany.myproject

(3) compile the files, for example, with javac -cp . com/mycompany/myproject/*.java (3) 编译文件,例如,使用javac -cp . com/mycompany/myproject/*.java 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, eg如果需要使用外部库(.jar 文件)进行编译,则需要使用-cp-classpath命令行参数给javac工具指定它们,例如

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, eg lib subdirectory of your main project directory.将所有外部库放在一个地方可能是个好主意,例如主项目目录的lib子目录。 And, by the way, the above javac command assumes unix-like environment.而且,顺便说一下,上面的javac命令假定了类 Unix 环境。 If you're on Windows, then you'll need to use ;如果您使用的是 Windows,则需要使用; for path separation.用于路径分离。

packages are just directories on the filesystem.包只是文件系统上的目录。 so your package: com.mycompany.util corresponds to a directory com/mycompany/util .所以你的包: com.mycompany.util对应于目录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 .例如,创建一个项目目录myproject并在myproject/src/下有你的 java 文件和包,在myproject/libs/下有你使用的myproject/libs/然后当你当前的工作目录是myproject执行java -cp .:libs/*.jar或与javac相同。

But I suggest you look into using ant or maven.但我建议您考虑使用 ant 或 maven。

You can get along just fine on the command line by using a packaging tool such as Ant or Maven .通过使用诸如AntMaven 之类的打包工具,您可以在命令行上相处得很好。 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. Maven 特别方便,因为它是一个更高级别的工具,它已经知道如何构建各种项目类型:命令行应用程序、web 应用程序、库等。它还通过从存储库下载它们来处理库依赖项。

Java Package 只是一个目录结构,因此创建一个 Package 的简单方法可以说在终端中的 com.organization.test 将是

mkdir -p com/organization/test

// 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 //创建一个空的java文件

$ 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. PS:你电脑上的目录结构和包名有关。 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.*).这意味着当您编辑 .java 文件时,它需要有一个包声明(您的包),否则您将拥有一个默认包(例如:java.util.*)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM