Linux 在命令行中制作一个java包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10971838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
making a java package in the command line
提问by gossfunkel
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). So please don't just tell me "Just use eclipse!" or something :P
虽然它可能是推荐的一种用途和用于编写高级 Java 项目的 IDE,但我个人更喜欢几乎完全运行命令行(使用 gedit 作为文本编辑器)。所以请不要只告诉我“只要使用 eclipse!” 或者什么: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.
我知道我在这里真的很尴尬,但我真的更喜欢在命令行中工作的控件。
采纳答案by Mattias Isegran Bergander
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
.
要包含库,请在编译和运行时将它们包含在类路径中。例如,创建一个 Project 目录myproject
,在该目录下有您的 java 文件和包myproject/src/
以及您在myproject/libs/
Then下使用的库,当您当前的工作目录myproject
执行java -cp .:libs/*.jar
或与javac
.
But I suggest you look into using ant or maven.
但我建议您考虑使用 ant 或 maven。
回答by David Harkness
You can get along just fine on the command line by using a packaging tool such as Antor 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.
通过使用诸如Ant或Maven 之类的打包工具,您可以在命令行上相处得很好。Maven 特别方便,因为它是一个更高级别的工具,它已经知道如何构建各种项目类型:命令行应用程序、Web 应用程序、库等。它还通过从存储库下载它们来处理库依赖项。
回答by Aleks G
There are three parts to it: (1) create directory structure; (2) indicate package in java file; (3) compile it.
它分为三个部分:(1)创建目录结构;(2)在java文件中指明包;(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:
例如,如果要创建 package 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
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.
如果需要使用外部库(.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, 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.
将所有外部库放在一个地方可能是个好主意,例如lib
您的主项目目录的子目录。而且,顺便说一下,上面的javac
命令假设了类 Unix 环境。如果您使用的是 Windows,那么您将需要;
用于路径分离。
回答by Vinamra Mattoo
Java Package is just a directory structure, so a simple way of creating a Package lets say com.organization.test in terminal will be
Java Package 只是一个目录结构,因此创建一个 Package 的简单方法可以说在终端中的 com.organization.test 将是
mkdir -p com/organization/test
回答by avrm
// 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. 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.*).
PS:你电脑上的目录结构和包名有关。这意味着当您编辑 .java 文件时,它需要有一个包声明(您的包),否则您将拥有一个默认包(例如:java.util.*)。