在 linux 中运行 java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9069581/
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
running a java program in linux
提问by Shenoy Tinny
I am trying to run a java program in linux environment. The program is structured as follows.
我正在尝试在 linux 环境中运行一个 java 程序。该程序的结构如下。
src (directory)
src(目录)
main (directory)
-- test.java
common (package)
-- a.java
-- b.java
主要(目录)
-- 测试.java
普通(包)
--a.java
-- b.java
Test.java is my main program.
Test.java 是我的主程序。
I used the following command to run the program from the src directory level.
我使用以下命令从 src 目录级别运行程序。
javac -cp "../../lib/netcdf.jar:/common/*.java" main/test.java
javac -cp "../../lib/netcdf.jar:/common/*.java" main/test.java
I am getting errors related to package common not found and all the classes under it are not found.
我收到与未找到包 common 相关的错误,并且未找到它下的所有类。
Can you please help me solve this.
你能帮我解决这个问题吗。
Thanks
谢谢
采纳答案by Shenoy Tinny
I had it compiled and running by using the following command
我使用以下命令编译并运行它
javac -classpath '.:../../lib/*' main/test.java
javac -classpath '.:../../lib/*' main/test.java
回答by Kevin
Java doesn't expand the glob (*
), unless it's the lone, last piece of a path (in which case it expands to all the jar files). It's been a while, but I believe you should be able to just leave the *.java
off entirely (-cp "../../lib/netcdf.jar:common"
). And it appears you were using /common
, which would make it look for a folder named common
in the root of your system.
Java 不会扩展 glob ( *
),除非它是路径的最后一段(在这种情况下它会扩展到所有 jar 文件)。已经有一段时间了,但我相信您应该能够*.java
完全停止 ( -cp "../../lib/netcdf.jar:common"
)。看来您正在使用/common
,这将使它查找common
在您的系统根目录中命名的文件夹。
回答by Tim Gee
There's a few problems here.
这里有几个问题。
Firstly, javac compilesyour code in class files (.class). It doesn't run the code, but converts the source code into a form that can be run.
首先,javac在类文件 (.class) 中编译您的代码。它不运行代码,而是将源代码转换成可以运行的形式。
When compiling code, all the .java files should be structured according to their package. So you can't arbitrarily decide to put some java files in one folder and some in another folder. For example, if a java file specifies "package com.mycompany" and your src directory is specified as src then the java file must be located in "/src/com/mycompany".
编译代码时,所有的 .java 文件都应该根据它们的包进行结构化。所以你不能随意决定将一些java文件放在一个文件夹中,而将一些放在另一个文件夹中。例如,如果一个 java 文件指定“package com.mycompany”并且您的 src 目录被指定为 src,那么 java 文件必须位于“/src/com/mycompany”中。
In your code, when you need to reference other code (usually external libraries) that have already been compiled into .class files, you can specify the classpath(which is why it is called as such). Note, I'll repeat this for clarity, this is not for .java files, but for .class files. Sometimes lots of .class files are packaged together into a single archive file, called a java archive, or 'jar'.
在你的代码中,当你需要引用已经编译成 .class 文件的其他代码(通常是外部库)时,你可以指定类路径(这就是它被这样调用的原因)。注意,为了清楚起见,我将重复这一点,这不是针对 .java 文件,而是针对 .class 文件。有时,许多 .class 文件被打包成一个单一的存档文件,称为 java 存档或“ jar”。
Also note, on unix systems that "/common" is an absolute path. If you want a relative path, you should specify "./common".
另请注意,在 unix 系统上,“/common”是绝对路径。如果你想要一个相对路径,你应该指定“./common”。
Additionally, the classpath separator is ";" not ":".
此外,类路径分隔符是“;” 不是 ”:”。
So in summary, if none of your java files specify an explicit package, simply put all your .java files in the same directory (I suggest ./src). Then run javac -sourcepath ./src *.java
or simply javac *.java
from the src directory.
所以总而言之,如果您的 java 文件都没有指定明确的包,只需将所有 .java 文件放在同一目录中(我建议 ./src)。然后运行javac -sourcepath ./src *.java
或直接javac *.java
从 src 目录运行。
There's more on managing source files and class files here.
还有更多关于管理的源文件和类文件在这里。
回答by arivazhagan
- Open terminal
- Type
java
- Display some packages
- You install this packages
- Now installed java packages
- Now type
java
- Type
javac
- Type
appletviewer
- You make new a directory=>
md arivu
cd arivu
gedit ex.java
- Now you save
- Return to terminal
- Compile:
javac ex.java
- Run:
java ex
- Graphical code:
- javac ex.java
- appletviewer ex.java
- 打开终端
- 类型
java
- 显示一些包
- 你安装这个包
- 现在安装的java包
- 现在输入
java
- 类型
javac
- 类型
appletviewer
- 你新建一个目录=>
md arivu
cd arivu
gedit ex.java
- 现在你保存
- 返回终端
- 编译:
javac ex.java
- 跑:
java ex
- 图形代码:
- javac ex.java
- 小程序查看器 ex.java