简体   繁体   中英

running a java program in linux

I am trying to run a java program in linux environment. The program is structured as follows.

src (directory)

  • main (directory)

    -- test.java

  • common (package)

    -- a.java

    -- b.java

Test.java is my main program.

I used the following command to run the program from the src directory level.

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). 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.

There's a few problems here.

Firstly, javac compiles your code in class files (.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. 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".

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 '.

Also note, on unix systems that "/common" is an absolute path . If you want a relative path, you should specify "./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.

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

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

  1. Open terminal
  2. Type java
  3. Display some packages
  4. You install this packages
  5. Now installed java packages
  6. Now type java
  7. Type javac
  8. Type appletviewer
  9. You make new a directory=> md arivu
  10. cd arivu
  11. gedit ex.java
  12. Now you save
  13. Return to terminal
  14. Compile: javac ex.java
  15. Run: java ex
  16. Graphical code:
    • javac ex.java
    • appletviewer ex.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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