0

I'm currently learning java from a book and I just reached packages. I've been saving all my files on my desktop and compiling/running programs from Mac's Terminal console.

John-MacBook-Pro:~ john$ cd desktop
John-MacBook-Pro:desktop john$ javac Learning.java.   
John-MacBook-Pro:desktop john$ java Learning

.... program executes and so on .....

Now I save my .java files into a package (create a new folder). Let's call the package 'book' And I'm told to run programs like this now:

 javac book/Learning.java
 java book.Learning

This works when I have one folder, sure, but when subclasses and more packages are added into that book folder how do I compile things deeper in? Not to mention how to run them afterwards?

The book might have assume prior knowledge so it just dives right in and tells me to setup CLASSPATH or use -classpath on my Macbook before attempting. I've tried various commands on terminal and it seems to compile sometimes where I have to manually change directory to open each folder (which is a lot of typed commands). Trying to run any classes always result in class not found. Every other answer seems to have some of the basic stuff setup already or is explained in terminology I don't understand yet.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Leosam
  • 33
  • 2
  • 1
    Possible duplicate of [How is the Java classpath set on a Mac?](https://stackoverflow.com/questions/5352584/how-is-the-java-classpath-set-on-a-mac) – Angelo Fuchs Jul 29 '18 at 16:52
  • Welcome to Stackoverflow! Be sure to take the [tour](https://stackoverflow.com/tour). Your question has already been asked here and will most likely be put on hold for that. So have a look at the link I prodvided and if it doesn't help, just type 'java classpath mac' in the search bar and you'll quickly find solutions to your problem. (Its usually faster then writing a whole new question :)) See you around. – Angelo Fuchs Jul 29 '18 at 16:55
  • Its not the same and none of them teaches what I'm asking. Especially not terminal commands. Knowing the concept and hands on is two different things ... – Leosam Jul 29 '18 at 17:09
  • After I searched 'java classpath mac' as I suggested, I found this on page 1: https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean/18093929#18093929 It goes very in depth about what is needed to run something from the command line. Mac is not special in this regard. The q&a I linked first also had an answer about the CLASSPATH variable and how its configured. I know the feeling of starting something where all the stuff makes no sense, yet still, your question already has answers here. – Angelo Fuchs Jul 29 '18 at 17:31
  • 1
    Yeah its hard trying to learn alone, guess I'll read up more and continue to break things down till I actually grasp it. – Leosam Jul 29 '18 at 18:37
  • Be aware that the `CLASSPATH` environment variable is something that is hardly ever used in Java, and is something that would better be avoided by explicitly specifying the classpath on the Java command line (using `-cp`) instead. – Mark Rotteveel Jul 31 '18 at 15:29

1 Answers1

-1

When more classes are added, you compile them all:

javac book/Learning.java book/chapter/Chapter.java ...

You run the main class exactly the same way:

java book.Learning

If you're not in the package where the root of the package tree is (i.e. your desktop directory), you pass it in the classpath:

java -classpath /users/Leosam/desktop book.Learning

Note that it works on macOS the same way as on any other platform.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255