• 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

How to run several classes at the same time in mac terminal

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am new to Java and I am currently reading "Head First Java" chapter 5.
I am struggling to run three complied classes.
Can anybody tell me what command to use to run several classes with two packages?

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Perhaps, it would help if .... Can you show us some of the stuff (commands) that you are struggling with? Or can you show us some of the code that you are trying to compile?

Henry
 
Aliya Khamzieva
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have three classes Game that contains the main method, SimpleDotCom and GameHelper
the Game class and SimpleDotCom are within chap05 package, GameHelper within a package helper that is in the directory chap05.

I have complied the three classes using javac *.java

Now I want to run the classes but I do not know how
I tried java Game
it does not work
Then I tried java chap05.Game
it also does not work and gives the following error

Exception in thread "main" java.lang.NoClassDefFoundError: helpers/GameHelper
at chap05.Game.main(Game.java:11)
Caused by: java.lang.ClassNotFoundException: helpers.GameHelper
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more

I can see that it has something to do with the package helpers but I don't know what
I will really appreciate your help
Game.png
[Thumbnail for Game.png]
SimpleDotCom.png
[Thumbnail for SimpleDotCom.png]
GameHelper.png
[Thumbnail for GameHelper.png]
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags when posting and post text not screenshots.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters the package is 'helpers', not 'chap05.helper', so it shouldn't be in the chap05 directory, it should be in its own directory, which should be called 'helpers'.

Essentially, the directory structure has to match your package structure.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Aliya. What the commenters above have told you is correct. That is, make your directory structure match your package structure (later, when you learn about jar files, this restriction will go away). And, yes, definitely use code tags. In fact, you'll get more help here if you do use them because, among other things, we can copy and paste your code into our own editors/IDEs and run it. That makes it possible to debug it. Not everyone will do that for you, but it happens, so you want to make it as easy as you can for people to do that.

You are definitely following a good path. Head First Java is a respected text. I believe the most recent edition is over ten years old, so some of what you are going to learn from it is out of date. That's not to say it's "wrong," because it isn't. What it means, though, is that the book cannot address some new features of the language that are very popular and reflect better practices than are in the book. It also means you are going to learn about some classes in the standard library that still exist (classes rarely just go away, as that would break a lot of working code all over the world), but no one writes new code anymore that uses them. That's because new classes that didn't exist when the book was written have superseded them. Don't let that worry you! You are learning basics and technique, rather than acquiring a lot of "lore" that will allow you to write production code. (If someone here points out that you are using an old method or an old class, just don't take it personally. They're just letting you know something that book can't tell you, so try to take it as the positive advice it is meant to be.)

Glad you are joining the fold of Java coders. You've taken your first step into a larger world.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

There is an easier solution about the downloaded classes from HFJ. If you look at the downloads, and compare them with the code printed in the book, you will find a tiny subtle difference. The downloaded code has package declarations in and the code in the book doesn't. It should be easy to find; it will be at the very beginning of the file. Simply change this
package chapter999;
to read
// package chapter999;
Then put all the .java files into the same directory and compile them without package names.

I don't think HFJ is actually out of date; it does after all describe Java5. I think it is fairer to say there are many new features which it cannot cover.
 
Aliya Khamzieva
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everybody for the help!
It is really amazing to be able to have people with expertise help you. I feel really exited about it.
Sorry that I did not know not about code tags. I will certainly use it in the future.

Campbell Ritchie, thank you! It worked! You put me out of misery.

It is a shame that the Head First Java is so old but I love the way it is written. Hopefully once I cover it, I will be able to study something more current.

Again thanks a lot!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aliya Khamzieva wrote:. . . Campbell Ritchie, thank you! . . .

That's a pleasure I have seen the same problem here before, so I simply had to remember the previous solution.

It is a shame that the Head First Java is so old but I love the way it is written. . . . something more current. . . .

There is nothing wrong with HFJ. You might consider this book by Cay Horstmann later.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic