• 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

Compiling Code with packages (OCA Study Guide Chapter 1 page 14 and 15)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

My name is Ashana.
Right now, I am studying Java programming so that I become a Web designer later (I know it takes more than only Java but its a start).
I am very new in the world of programming and especialy in Java.
I am studying the Book OCA Java SE 8 programmer I Study Guide and I am stuck at chapter 1 page 14 and 15.
It tells how to compile code with packages, but I don't understand what they mean by creating two files in Windows Setup:
The page says:

Create two files:

C:\temp\packagea\ClassA.java
C:\temp\packageb\ClassB.java

Then type this command:

cd C:\temp

Then to compile type this command:
javac packagea/ClassA.java packageb/ClassB.java


I tried to create a file in NetBeans Version 8.2, but it seems I am doing something wrong in this program.
Or should I do this in another program like Eclipse?

I am trying to understand this, but I keep stuck on these pages.
Does anyone have tips how to handle this?

 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the way the instructions are described, I'd assume you are supposed to use a text editor to create the .java files, not an IDE.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Ashana Hurkens wrote:. . . I am studying Java programming  . . . the Book OCA Java SE 8 programmer I Study Guide . . . .

Does that mean you are a beginner at Java®? Maybe you have found the wrong book. Please provide more details about the book, including authors, but exam study guides are not intended as beginner's tutorials. There aren't many good beginners' books; I suggest Head First Java by Sierra and Bates (O'Reilly 2005, =HFJ), or maybe Cay Horstmann's beginner's book called something like Java for the Impatient. Beware: HFJ is old, so buy a second‑hand copy and don't pay full price. Horstmann has written at least two books with similar titles.

. . . I tried to create a file in NetBeans . . . should I do this in another program like Eclipse? . . .

All IDEs will create their own package and directory structure, so you can only do that exercise via the command line, neither NetBeans nor Eclipse.

If you are a beginner, you might do well to delay learning packages for a few weeks. Have a look at the Java™ Tutorials about packages, anyway.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can find details of Horstmann's books on his website. I think I was mistaken about the “Impatient” books; they may not be suitable for beginners after all.
 
Ashana Hurkens
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the tips!
I am going to check them out later

The book was written by Jeanne Boyarsky and Scott Selikoff.
I received this book when I started doing the course Java programming.

I first studied the book Head first Java by Sierra Bates and it is indeed an old version.
But I really recommend this book, because it teaches you everything about Java and things to keep in mind.

Sorry if I forgot to mention that in my first message, I am indeed a beginner in Java.
And according to the book Oracle Certified Associate Java SE 8 programmer I Study guide by Jeanne Boyarsky and Scott Selikoff it was recommendable to study a reference book about Java first, such as Head first Java.

Maybe this will help more about the instructions on page 14 and 15 of the OCA (Oracle Certified Associate Java SE 8 programmer I Study Guide)?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. You would probably do well to learn ordinary programming and you can get away without writing your own packages for three months.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have the same question - on page 14-15
it tells how to compile code with packages, but I don't understand what they mean by creating two files in Windows Setup:
The page says:

Create two files:

C:\temp\packagea\ClassA.java
C:\temp\packageb\ClassB.java

Then type this command:

cd C:\temp

Then to compile type this command:
javac packagea/ClassA.java packageb/ClassB.java
i tried in command prompt but in vain.
how to proceed?
 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, here is the explanation of what you need to do. (or at least how i did, i will show you my method)

Create a Directory on your Desktop which you can call LearningJava, in this Directory there will be other 2 Directoryes WorkSpace and WorkSpaceRow.
You will use the WorkSpace Directory for all the projects and files of the book and you will use WorkSpaceRow just for try random things, this way you can distinguish 2 workspaces.
Now you want to make a try, so just work in your WorkSpaceRow, which will have another Directory called Chapter1.

This is the structure:

C:\Users\yourUsername\Desktop\LearningJava

Now in LearningJava you will found:

LearningJava
          |
          |____WorkSpace
          |                 |
          |                 |_______Chapter1
          |                 |                                                    <----------- Here will be the Projects and files of the Book
          |                 |_______Chapter2
          |                 |
          |
          |
          |  
          |____WordSpaceRow  
          |                |
          |                |_____Chapter1                                <----------- Here you can do your files to try it and have in other location than the files of the book example
          |                |             |_______packagea
          |                |             |
          |                |             |_______packageb
          |                |             |
          |                |             |
          |                |             |
          |                |             |
          |                |             |

Now for write the code you should use Notepad++, So the first thing to do it`s searching Notepad++ in google, download and install it.
When you done, just open Notepad++
I assume you Created all the Directoryes how i told you.

Now you should write ClassA.java in Notepad++ and then you go on the top menu, Save as -> ClassA.java(you need to write the name of the file with .java extention), then below the Name which you type you need to click and then choose Save as All Types(and not text file), save this file in the packagea directory.

After this step, you can see your ClassA.java file in the Directory which is on Desktop->LearningJava->WorkSpaceRow->Chapter1->packagea (so here you can just click click and go to see your file)

Notice: C:\temp of the Book, it`s equivalent of your C:\Users\yourUsername\Desktop\LearningJava\WorkSpaceRow\Chapter1

Now that you had write your Class and save it in your Chapter1/packagea, you need to compile it.
For compile it you need to open the cmd
cmd = Command Prompt

so you go on the left corner of your Desktop and type in the SearchBar cmd, then you hit enter
Now, you just open your Command Prompt, which is a console, so you can go everywhere in your pc by typing and not by clicking.
When you open your cmd, usually you will be in
C:\Users\yourUsername  -> where yourUsername it`s just your username which is not equal to mine.

Now you can access every directory in your computer. If you wat to "doubleClick on a directory" enter in a directory you just type cd theDirectoryName.
I told you that you are now in  C:\Users\yourUsername if you want to see all the files and directoryes(which is usefull to understand where you can go from where you are rigthnow) you just need to type dir and hit enter. (just try it)
After you write dir and hit enter you will see, that you can see your Desktop directory. Now let`s move there.
You type: cd Desktop and hit enter
now you will see, your path changed from C:\Users\yourUsername to C:\Users\yourUsername\Desktop, so now you need to go in the directory where is your file that you want to compile
To do this you can go directly typing from where you are the remaining path: in this case just type cd LearningJava\WorkSpaceRow\Chapter1

Now you can compile the ClassA.java which is in the packagea package.

You just type: javac packagea/ClassA.java
Now you can also go visually(click click) in the desktop, learningjava, workspacerow,chapter1, packagea directory, and you will see that after you compile you will have another file called ClassA.class(which is the bytecode, which is runnable by JavaVirtualMachine which is launched by java command)

After this you can compile your ClassB.java by typing javac packageb/ClassB.java
Now you just compiled the files individually, now just delete the ClassB.class and ClassA.class files, and try again but this time you can compile in just 1 single command like that

javac packagea/ClassA.java packageb/ClassB.java (this is waht the book says to do)

Now that you had compiled the files to run the files you should tyupe this:

java packagea.ClassA (if you want to run the ClassA program)
java packageb.ClassB (if you want to run the ClassB program)

Notice, if you want to run a class, it must have the main metod. In your case(i have also the book but maybe a different one, i have for java 11) maybe in your example ClassA don`t have any main method, but it is used when ClassB is launched, in this case, don`t try to run ClassA.
You just need to run ClassB.
So just type java packageb.ClassB

Now, if you have a .java file but it`s in no package you just run this command:

java ClassB  (to run ClassB if it`s not in a package)
Basically the package it`s nothing else than a directory. But when you want to run a program, you need to be in the Directory which contain the package, and NOT run the command from the package location.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Damian wrote:. . . Create a Directory on your Desktop . . .

I would recommend you put it elsewhere, viz. the first directory you get when you open a new command line/terminal.You only need line 1 once, and you can give the directory any sensible name. You can use the same instructions on Linux and Windows®. Otherwise, that is good stuff And welcome to the Ranch
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic