• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using "javac" command line

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic