1

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

5
  • You should just be able to javac the main class. The compiler will draw in the resources in needs based on the imports in the file. Commented Jan 30, 2012 at 19:32
  • Try cding into the directory then running the command. Commented Jan 30, 2012 at 19:33
  • 1
    what are you importing in your source file? classes and packages that you import are somewhere? are they linked in your classpath? how? Commented Jan 30, 2012 at 19:34
  • @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. Commented Jan 30, 2012 at 19:38
  • I think that Kevin answer is the way to go in your case Commented Jan 30, 2012 at 19:45

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
@ShenoyTinny Oh, "common" is really a jar file, "common.jar"? make the common in the cp common.jar.
2

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.

Comments

2

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

Comments

-3

  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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.