• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Installing Java to macOS Sierra  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. Im new to JAVA and Im currently reading the book Head First Java. I have installed java 8 in my Mac (macOS Sierra 10.12). I tried the HelloWorld example but it says "file not found." I used textedit and saved it as "HelloWorld.java" on my desktop under test folder.

Please help. Thanks.
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I tried the HelloWorld example but it says "file not found." I used textedit and saved it as "HelloWorld.java" on my desktop under test folder.


What exactly did you try when you say "tried"?

Usually you would do these
1) Compile with the javac command
2) Execute the code with the java command.

So which one is giving you problems? Also can you copy paste the exact command you are trying to use?
 
Heddy Palapar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the "HelloWorld" example.



I saved it as "HelloWorld.java" onto my desktop/test folder. Then I use the javac command on the terminal. This is what the error looks like:

Heddys-MBP:~ hedz$ javac HelloWorld.java
javac: file not found: HelloWorld.java

I know I missed a critical step. Just don't know what
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to provide the fully qualified path for the source file. In your case it is Desktop/test/HelloWorld.java

So to successfully run the javac command from anywhere, you need
javac ~/Desktop/test/HelloWorld.java

Recommended reading: http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javac.html#examples
 
Heddy Palapar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was able to run the javac without any errors. I was basically pointing to the wrong location!  And the HelloWorld.class file is created.

But when I type in the command: " java HelloWorld"I got this error:

Heddys-MBP:test hedz$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
Heddys-MBP:test hedz$

What did I do wrong?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Navigate to where the class file is located
cd ~/Desktop/test
Now run java HelloWorld
Note: You should NOT give the .class extension to this command.

The problem is the JVM does not know where your class files are. So you navigate to the directory where they are located (cd ~/Desktop/test) and then execute them.
To simplify this process you can set the class path (path at which the JVM can expect to find class files)
More on it here
https://coderanch.com/wiki/660138/Set-Classpath

 
Heddy Palapar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I change the path using cd ~/desktop/test. So I am now on the directory where my HelloWorld.class file is at. Still when I enter the code "java HelloWorld" I encounter the same problem

Heddys-MBP:test hedz$ java HelloWorld
Error: Could not find or load main class HelloWorld
Heddys-MBP:test hedz$

thanks for your patience
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you show us the contents of your ~Desktop/test?
Just run the ls command from the terminal and copy paste the contents
 
Heddy Palapar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Heddys-MBP:test hedz$ ls
HelloWorld.class HelloWorld.java
Heddys-MBP:test hedz$
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange. I replicated it exactly the same on my machine and it runs successfully

maneeshMac:test maneesh$ ls
HelloWorld.class HelloWorld.java
maneeshMac:test maneesh$ java HelloWorld
Hello World from a Mac!
maneeshMac:test maneesh$



Initially I thought you might face a problem because of the upper case C in "Class" but then your compilation would have failed.

Anyone else got a clue?

PS. In the meanwhile since this is not a Mac problem, I will crosspost your topic to a more appropriate forum for you.
 
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Heddy,
the problem is your class path does not contain a current directory as a source to read class files from there.so simply correct it and have fun.i am attaching a reference to 2 threads for making it simple for you,kindly go through both of these for the better understanding:
https://coderanch.com/wiki/659740/create-Java-program
https://coderanch.com/wiki/660138/Set-Classpath

Hope it helps!!!

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 483
23
AngularJS Eclipse IDE Firefox Browser Java Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the 2nd link follow the procedure for Unix variant.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!