aspose file tools*
The moose likes Beginning Java and the fly likes Using Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using "javac" command line" Watch "Using "javac" command line" New topic
Author

Using "javac" command line

Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
I started creating my programs in Notepad and compiling them from the command line using the "javac" command so that I can get better at Java syntax.

The problem is when I use self-made packages and import them, the compiler cannot find the packages? I keep getting the same error "package mypackage does not exist". The same code works when I type it in Netbeans--I assume this has to do with classpath, sourcepath, and file locations. I just can't figure out how to do this in Notepad.

Any ideas?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

Let's make this little directory structure (for simplicity)

C:\Java\Projects\Test\

Let's say this is the 'base directory' for your application, up to now all your .java and .class files ended up in this directory. Now you add a class with a package names "mypackage". In order to use it, then you need the 'package folder':

C:\Java\Projects\Test\mypackage\

You would put the .java file into that package directory:


C:\Java\Projects\Test\mypackage\MyClass.java

Then, in the command line you would navigate to the 'base directory:

C:\>cd C:\Java\Projects\Test
C:\Java\Projects\Test>

Now, type in the compile command, providing the full path of the class file, relative to the command line:
C:\Java\Projects\Test>javac -cp . mypackage/MyClass.java

See: You compile from the parent directory of the package, then provide the package name as a directory prefix to the .Java file name. If you have multiple files to compile in the package you use:

C:\Java\Projects\Test>javac -cp . mypackage/*.java



Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to JavaRanch

NetBeans has its own structure where it puts the classes, so you might have to copy and paste the code.
Don't use NotePad for programming; use something like jEdit, NotePad++ or NotePad2. They are far better for programming.

You find that the javac tool seems to "assume" that the packages have already been compiled. Try compiling the classes nearest the "main" method last, and those "farthest from" the "main" method first.

I note Steve Luke has given some useful advice. You will find more here in the Java™ Tutorials, and here, and on JavaRanch similar questions come up every now and again: I found an old post of my own, with several links in.
Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
Thanks. I've been bangin my head on that for quite a while....

something else that I don't completely understand about this line:



Are we saying that the classpath is C:\Java\Projects\Test? I thought that the classpath would be where the ".class" file is located which in this example would be C:\Java\Projects\Test\mypackage.

Your solution works this is just for my personal clarification. Thanks.
Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
Campbell Ritchie wrote:Welcome to JavaRanch

NetBeans has its own structure where it puts the classes, so you might have to copy and paste the code.
Don't use NotePad for programming; use something like jEdit, NotePad++ or NotePad2. They are far better for programming.


thanks I've heard of these...I definitely look into it now.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

Paul Wam wrote:Thanks. I've been bangin my head on that for quite a while....

something else that I don't completely understand about this line:



Are we saying that the classpath is C:\Java\Projects\Test? I thought that the classpath would be where the ".class" file is located which in this example would be C:\Java\Projects\Test\mypackage.

Your solution works this is just for my personal clarification. Thanks.


We are defining the root of the classpath as being C:\Java\Projects\Test. All classes and packages must be found in the classpath, and are found relative to the root of the classpath.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using "javac" command line
 
Similar Threads
A colleague's question about a large project
Which is best and easiest Development Environment?
Classpath
JAVAC not found ??? I cant compile - help
Major problem with GuessGame code, ch02 of "Head First Java, 2nd edition, covers Java 5.0"