• Post Reply Bookmark Topic Watch Topic
  • New Topic

Installing Java to macOS Sierra  RSS feed

 
Heddy Palapar
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.
 
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
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.
 
praveen kumaar
Ranch Hand
Posts: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • 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: 450
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the 2nd link follow the procedure for Unix variant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!