Forums Register Login

HelloWorld in terminal

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
The program javac takes a filename so you need the path. The program java takes a class name so you need the dot.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.

1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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"
+Pie Number of slices to send: Send
That was the difference maker, Thanks to all!!
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 594 times.
Similar Threads
Inconsistent behaviour of -classpath between java & javac
javac: no classpath found
Trying to run my first program: java.lang.NoClassDefFoundError
Installation Problem-Help with running compiled code in command line.
Something is wrong with my Java on LInux...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:54:09.