简体   繁体   English

在Linux中运行Java程序

[英]running a java program in linux

I am trying to run a java program in linux environment. 我试图在Linux环境中运行Java程序。 The program is structured as follows. 该程序的结构如下。

src (directory) src(目录)

  • main (directory) 主(目录)

    -- test.java -test.java

  • common (package) 普通(包装)

    -- a.java -一个Java

    -- b.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. 我收到与找不到包通用包相关的错误,并且找不到它下面的所有类。

Can you please help me solve this. 你能帮我解决这个问题吗?

Thanks 谢谢

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). Java不会扩展glob( * ),除非它是路径的唯一的最后一部分(在这种情况下,它会扩展到所有jar文件)。 It's been a while, but I believe you should be able to just leave the *.java off entirely ( -cp "../../lib/netcdf.jar:common" ). 已经有一段时间了,但是我相信您应该可以完全不使用*.java-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. 看来您在使用/common ,这会使它在系统根目录中查找一个名为common的文件夹。

There's a few problems here. 这里有一些问题。

Firstly, javac compiles your code in class files (.class). 首先,javac在类文件(.class)中编译您的代码。 It doesn't run the code, but converts the source code into a form that can be run. 它不运行代码,而是将源代码转换为可以运行的形式。

When compiling code, all the .java files should be structured according to their package. 编译代码时,所有.java文件都应根据其包进行结构化。 So you can't arbitrarily decide to put some java files in one folder and some in another folder. 因此,您不能随意决定将某些Java文件放在一个文件夹中,而将某些Java文件放在另一个文件夹中。 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文件指定“ 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). 在您的代码中,当您需要引用已经编译成.class文件的其他代码(通常是外部库)时,可以指定类路径 (这就是为什么要这样称呼)的原因。 Note, I'll repeat this for clarity, this is not for .java files, but for .class files. 注意,为清楚起见,我将重复此操作,这不是针对.java文件,而是针对.class文件。 Sometimes lots of .class files are packaged together into a single archive file, called a java archive, or ' jar '. 有时,许多.class文件会打包在一起,形成一个存档文件,称为Java存档或“ jar ”。

Also note, on unix systems that "/common" is an absolute path . 还要注意,在unix系统上,“ / common”是绝对路径 If you want a relative path, you should specify "./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). 因此,总而言之,如果您的java文件均未指定显式包,只需将所有.java文件放在同一目录中(我建议使用./src)。 Then run javac -sourcepath ./src *.java or simply javac *.java from the src directory. 然后从src目录运行javac -sourcepath ./src *.java或仅运行javac *.java

There's more on managing source files and class files here . 这里还有更多有关管理源文件和类文件的信息

I had it compiled and running by using the following command 我使用以下命令编译并运行了它

javac -classpath '.:../../lib/*' main/test.java javac -classpath'。:../../ lib / *'main / test.java

Source: http://en.wikipedia.org/wiki/Classpath_%28Java%29 来源: http : //en.wikipedia.org/wiki/Classpath_%28Java%29

  1. Open terminal 打开终端
  2. Type java 键入java
  3. Display some packages 显示一些包裹
  4. You install this packages 您安装此软件包
  5. Now installed java packages 现在安装的Java包
  6. Now type java 现在输入java
  7. Type javac 键入javac
  8. Type appletviewer 键入appletviewer
  9. You make new a directory=> md arivu 您创建一个新目录=> md arivu
  10. cd arivu
  11. gedit ex.java
  12. Now you save 现在保存
  13. Return to terminal 返回码头
  14. Compile: javac ex.java 编译: javac ex.java
  15. Run: java ex 运行: java ex
  16. Graphical code: 图形代码:
    • javac ex.java javac ex.java
    • appletviewer ex.java appletviewer ex.java

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

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