• Post Reply Bookmark Topic Watch Topic
  • New Topic

HelloWorld in terminal  RSS feed

 
Edward Wright
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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.

 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Liutauras Vilda
Sheriff
Posts: 4927
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
That was the difference maker, Thanks to all!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!