ejecutando un programa java en 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.

Muchas Gracias

preguntado el 30 de enero de 12 a las 19:01

Deberías poder javac the main class. The compiler will draw in the resources in needs based on the imports in the file. -

Try cding into the directory then running the command. -

what are you importing in your source file? classes and packages that you import are somewhere? are they linked in your classpath? how? -

@thermz : How do I link the netcdf jar and the common package to the classpath? when I compile like javac main/test.java, it throws errors realted to classes in netcdf jar files. so, when I link them to the classpath like, javac -cp "../../lib/netcdf.jar" main/test.java, I get errors related to package common not found. -

I think that Kevin answer is the way to go in your case -

4 Respuestas

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.

Respondido el 30 de enero de 12 a las 23:01

I ran the follwing commnand per ur suggestion: javac -cp "../../lib/netcdf.jar:common" main/test.jar. But doesnt seem to work though. I am still getting the errors - Shenoy Tinny

@ShenoyTinny Oh, "common" is really a jar file, "common.jar"? make the common in the cp common.jar. - Kevin

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

Respondido el 30 de enero de 12 a las 23:01

I had it compiled and running by using the following command

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

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

Respondido 02 Feb 12, 02:02

  1. Terminal abierta
  2. Tipo java
  3. Display some packages
  4. You install this packages
  5. Now installed java packages
  6. Ahora escribe java
  7. Tipo javac
  8. Tipo 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. Compilar: javac ex.java
  15. Ejecutar: java ex
  16. Graphical code:
    • javac ex.java
    • appletviewer ex.java

Respondido 02 Abr '13, 15:04

This answer doesn't look completely relevant to the original question. - lucrussell

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.