Installing Java to macOS Sierra
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
Please help. Thanks.
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome to the Ranch.
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?
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
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
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?
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Heddys-MBP:test hedz$ java HelloWorld
Error: Could not find or load main class HelloWorld
Heddys-MBP:test hedz$
thanks for your patience
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Could you show us the contents of your ~Desktop/test?
Just run the ls command from the terminal and copy paste the contents
Just run the ls command from the terminal and copy paste the contents
Heddy Palapar
Greenhorn
Posts: 5
posted 8 years ago
Heddys-MBP:test hedz$ ls
HelloWorld.class HelloWorld.java
Heddys-MBP:test hedz$
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Heddys-MBP:test hedz$ ls
HelloWorld.class HelloWorld.java
Heddys-MBP:test hedz$
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Strange. I replicated it exactly the same on my machine and it runs successfully
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.
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.
posted 8 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
| Destiny's powerful hand has made the bed of my future. And this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |











