I am trying to run a java program in linux environment. The program is structured as follows.
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.
preguntado el 30 de enero de 12 a las 19:01
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 en la raíz de su sistema.
There's a few problems here.
Firstly, javac compila 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 'tarro'.
Also note, on unix systems that "/common" is an absolute camino. 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 o simplemente
javac *.java from the src directory.
There's more on managing source files and class files aquí.
- Terminal abierta
- Display some packages
- You install this packages
- Now installed java packages
- Ahora escribe
- You make new a directory=>
- Now you save
- Return to terminal
- Graphical code:
- javac ex.java
- appletviewer ex.java