• 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

HelloWorld in terminal

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following is the program I'm trying to run in terminal.

I am using TextWrangler editor and have saved the file. I have copied the path of the file into terminal and am pretty sure that is the directory I am in. It seems I can compile by running but when I try executing it I get "Could not find or load main class. I have tried including the packageDOTclassName but continue getting that error. I suspect that when you create a program in an IDE that other files are created automatically and that in terminal you have to do that manually.

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program javac takes a filename so you need the path. The program java takes a class name so you need the dot.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which directory is the hello world .java file in?
If you compile the .java file without instructions to create a new directory, it will be created in the same location. The folder structure of the .class files must correspond to the package structure, so the examples.HelloWorld.class file needs to be in the firectory called examples.

An IDE will automatically create the files in the correct locations; if you are compiling by hand, you must instruct the javac tool where to put the .class files. Start by looking at this old post and the Java™ Tutorials.
 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:The program javac takes a filename so you need the path. The program java takes a class name so you need the dot.


I've tried using dot operator but I'm getting the same response.

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your package information needs to match your folder (directory) structure. So in your situation, you need an "examples" folder. You can use javac from anywhere as long as the filename and path are correct, but you must execute java from the project folder (the one that has "examples" in it) and you must use the "dot" name.
 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Your package information needs to match your folder (directory) structure. So in your situation, you need an "examples" folder. You can use javac from anywhere as long as the filename and path are correct, but you must execute java from the project folder (the one that has "examples" in it) and you must use the "dot" name.


So by doing this, when I look in finder I should see a folder named examples as well correct?
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Lets assume you're in "Documents/".
2. By being in Documents/ you should see "examples" folder - this is your package (don't step in it, stay where you are)
3. So to execute program, you should write: "java examples.HelloWorld"
 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was the difference maker, Thanks to all!!
 
Hold that thought. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic