如何编译和运行此文件夹结构 [英] How to compile and run with this folder structure
问题描述
我的java源文件位于lib文件夹中的src / net / ...文件夹和.jar文件中。如何使用命令行编译和运行这些文件而不编写构建脚本?
让我们说你的代码文件在
[someDirectory]
|
+ - [lib]
| |
| + -someLib.jar
| + -someOtherLib.jar
| + -...
|
+ - [src]
|
+ - [净]
|
+ - [name]
|
+ - [1]
|
+ - [2]
|
+ - [主要]
|
+ -Main.java< - 要编译
并执行
的代码
然后如果您的控制台在
someDirectory>
你可以用
$ b <$ p $编译它p>
someDirectory> javac的-cp lib\ * src\\\
et\\\
ame\one\two\main\Main.java
但这会在与相同的目录中生成
所以要从 Main.class
文件Main.java net.name.one.two.main.Main
类执行代码,你需要包含 src
到classPath的目录,因为这个目录包含放置 Main
类的包,所以你需要使用命令
someDirectory> java -cpsrc; lib\ *net.name.one.two.main.Main
但最好将类文件与源文件分开。为此,您可以在编译应该具有已编译类文件的pass目录时添加 -d
(目录)参数。因此,首先在与 src
目录相同的级别创建类
目录并执行
'pre>
someDirectory> javac的-d 类 -cp lib\ * src\\\
et\\\
ame\one\two\main\Main .java
现在能够执行 Main
类而不是通过 src
目录创建混淆,你必须添加 classes
目录,这更直观。
someDirectory> java -cpclasses; lib\ *net.name.one.two.main.Main.java
I have my java source files in src/net/... folders and .jar files in lib folder. How to compile and run this files with command line without writing build script ?
Lets say you have your code files in
[someDirectory]
|
+-[lib]
| |
| +-someLib.jar
| +-someOtherLib.jar
| +-...
|
+--[src]
|
+-[net]
|
+-[name]
|
+-[one]
|
+-[two]
|
+-[main]
|
+-Main.java <- code you want to compile
and execute
then if your console is in
someDirectory>
you can compile it with
someDirectory>javac -cp "lib\*" src\net\name\one\two\main\Main.java
but this will produce Main.class
file in same directory as Main.java
so to execute code from net.name.one.two.main.Main
class you would need to include src
directory to classPath because this directory contains package that Main
class is placed, so you would need to use command
someDirectory>java -cp "src;lib\*" net.name.one.two.main.Main
But it is good practice to separate class files from source files. To do this you can add -d
(directory) parameter while compiling pass directory which should have compiled class files. So first create your classes
directory at the same level as src
directory and execute
someDirectory>javac -d "classes" -cp "lib\*" src\net\name\one\two\main\Main.java
and now to be able to execute your Main
class instead creating confusion by src
directory to classPath you will have to add classes
directory which is more intuitive.
someDirectory>java -cp "classes;lib\*" net.name.one.two.main.Main.java
这篇关于如何编译和运行此文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!