tags:

views:

85

answers:

5

I'm new to working with Java from the command line and I don't get it. I read previous CLASSPATH questions but still didn't get my problem to work.

I have the following class in C:\Temp\a\b\c

package a.b.c;

public class Hello
{
        public static void main(String args[])
        {
           System.out.println("Hello World!");
        }
}

The package name is intentional. I compiled it fine and I put the Hello.class file inside C:\Temp\a\target

Now in the command line I go to C:\Temp\ and execute the following:

java -cp .\a\target a.b.c.Hello

It complains that it cannot find the class a.b.c.Hello

Thanks in advance!!

+2  A: 

and I put the Hello.class file inside C:\Temp\a\target

This is wrong. It should be placed in the same folder as the .java file. The source code itself is declared to be in the package a.b.c; so, the .class file should really be kept in \a\b\c folder.

Then, to execute it just do:

C:\Temp>java -cp . a.b.c.Hello
BalusC
I do it intentionally because other projects I'm working on don't have the class files in the same folder as the source files. I'm trying to learn from this simple example for the bigger projects.
Then you need to change the `package` declaration of the code to match `a.target`.
BalusC
Well, not necessarily in the *same* folder, but in the same folder structure.
OscarRyz
Yes, it is working now after I fixed the src and target to have the same folder structure. Thanks!
You're welcome. Since you're new here, don't forget to mark the most helpful answer to accepted. See also http://stackoverflow.com/faq.
BalusC
+1  A: 

CLASSPATH tells Java where to search for programs Where to look? The Java runtime system needs to know where to find programs that you want to run and libraries that are needed. It knows where the predefined Java packages are, but if you are using additional packages, you must tell specify where they are located. CLASSPATH. Specifying where to search for additional libraries in Windows is easily done by seeting the environment variable CLASSPATH, which Java uses to see where it should find Java programs and libraries. Automatic? Some IDEs tell the Java runtime system where to search, and double-clickable Jar (Java Archive) files don't have this problem When do you need it? If you compile "by hand" (typing javac and java in a command window), or sometimes with TextPad or other editors.

First of all if you compile from c:\temp\a\b\c\Hello.java then the .class will get generated in c:\temp\a\b\c\Hello.class now if you want to run it just go back to c:\temp and try running java a.b.c.Hello
For more on classpath have a look

org.life.java
A: 

Avoid "putting" the classfiles anywhere. The following should work:

javac -d c:\temp c:\temp\a\b\c\Hello.java
# creates Hello.class in c:\temp\a\b\c
java -cp c:\temp a.b.c.Hello
Hemal Pandya
Got it now... thanks!
A: 

Java will try to search for a directory structure a\b\c from starting in target and as you notice, it wont work.

Move the whole directory into target and you'll be fine, it should look like:

 C:\Temp\a\target\a\b\c\Hello.class

You may compile it with the -d option which tall the compiler where to put the class file.

Many project structures are like this.

C:\whatever\projectname\src
C:\whatever\projectname\classes
C:\whatever\projectname\bin
C:\whatever\projectname\lib
C:\whatever\projectname\doc

That way you can always step on your project directory and type:

javac -d classes src\*.java 

Which will compile all the sources in the src directory and will place them in the classes directory.

Then execute your program:

java -cp classes a.b.c.Hello 

You may optionally place required jars in lib

This works pretty fine for small programs ( < 10 src files and 2 - 3 jar libraries ) If it grows beyond that, you could probably use an IDE or ant

The good thing about following this project structure is that some IDES ( as IntellJ idea ) just pick them very easily when you create a new project. You select "Create project from existing sources" and then you can continue from there.

I like compiling and editing at the command line a lot!!

OscarRyz
This is very helpful! thanks!
A: 

To expand on BalusC's point: the classpath defines a "root". When java is looking for your classes, it will start at each root (or jar) in your class path and drill down through the directories to match the package strucutre. You still need to have you class in a directory structure that matches its package name. In your case, to execute

java -cp .\a\target a.b.c.Hello

you would move the file to

.\a\target\a\b\c\Hello.class

Years ago, I too found this baffling.

Alan Arvesen
Got it now... Thanks!