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.
Java is used both online and offline in Windows, OS X and Linux. The online use involves Java programs, typically referred to as 'applets', embedded in a web page. All the security issues regarding Java involve applets. Offline, Java is used by applications installed in your operating system. Therefore, I simply added the path to the latest java version (in my case this is /Library/Internet. Now the latest version is used, when you use java in the shell.
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: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.LeosamLeosam
1 Answer
When more classes are added, you compile them all:
You run the main class exactly the same way:
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:
Note that it works on macOS the same way as on any other platform.
JB NizetJB Nizet