John Andrew-Mike

Greenhorn
+ Follow
since May 19, 2021
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Andrew-Mike

Hey, Guys, please help. I am lost. I get "java: unreachable statement". Could you tell me, why?

2 years ago

Stephan van Hulst wrote:Good on ya for sticking with it. Have a cow!



Cool! Thanks!
2 years ago

Stephan van Hulst wrote:Hold on. I'm glad it worked, but it's not correct.

It's very likely that the src/ folder acts as a root folder for your package hierarchy. This means that the Main class must be part of the com.company package. The fact that Main.class is output in the bin/ folder directly means you haven't declared it as being inside the com.company package.

While it might work now, you will run into problems as soon as your Main class starts referencing other classes.

The solution is to put the following statement at the top of your source file, and then use my original java command to run the application:

Double-check that Main.class is output in bin/com/company/.



Ok, I put the statement in the source, did everything again and works perfectly. Thank you, Stephan, your my today's hero. Thank you.  
2 years ago

Stephan van Hulst wrote:Can you tell us what's inside the bin/ folder? Is there a Main.class inside the bin/ folder, or is there a com/company/Main.class file in the bin/ folder?



Stephan, that was the case. The file is Main.class, so I changed it in the command and it works! I just almost don't believe it
Thank you so much for your great help and patience. I really appreciate that.
2 years ago

Stephan van Hulst wrote:Please read my instructions carefully.

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:



I do everything you tell me, really!
2 years ago

Stephan van Hulst wrote:

John Andrew-Mike wrote:javac: directory not found: bin


This means you must make an empty bin folder in the project3 folder.



Ok. I created the bin and it helped with the first command (since it shown no error), but still the program does not run and it can't find it.

C:\Users\Glopn_23\IdeaProjects\project3>javac -sourcepath src -d bin src/com/company/Main.java

C:\Users\Glopn_23\IdeaProjects\project3>java -cp bin com.company.Main
Error: Could not find or load main class com.company.Main
2 years ago

Stephan van Hulst wrote:Again, your current working directory needs to be the project3 folder.

The src/com/company/Main.java path that you pass to the javac command is relative to the project3 folder, so that's why that folder needs to be the current working directory.



I tried also this :

C:\Users\Glopn_23\IdeaProjects\project3>javac -sourcepath src -d bin src/com/company/Main.java
javac: directory not found: bin
Usage: javac <options> <source files>
use -help for a list of possible options
2 years ago

Stephan van Hulst wrote:Again, your current working directory needs to be the project3 folder.

The src/com/company/Main.java path that you pass to the javac command is relative to the project3 folder, so that's why that folder needs to be the current working directory.



still the same.

C:\Users\Glopn_23\IdeaProjects\project3>javac Main.java
javac: file not found: Main.java
Usage: javac <options> <source files>
use -help for a list of possible options

C:\Users\Glopn_23\IdeaProjects\project3>java Main
Error: Could not find or load main class Main
2 years ago

Stephan van Hulst wrote:

John Andrew-Mike wrote:but the javac I did myself worked. it created Main.class. so why doesn't it run?


Because you're in the wrong working directory and didn't specify a classpath.



Ok I changed the directory. and still the same problem, nothing changed

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>
C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>javac Main.java

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>java Main
Error: Could not find or load main class Main

I tried also:

C:\Users\Glopn_23\IdeaProjects\project3\src\com\company>javac -sourcepath src -d bin src/com/company/Main.java
javac: file not found: src\com\company\Main.java
Usage: javac <options> <source files>
use -help for a list of possible options
2 years ago

Stephan van Hulst wrote:

John Andrew-Mike wrote:I put the first in cmd and received: "javac: file not found: src\com\company\Main.java


Because you didn't run the javac command from the project3 folder.



but the javac I did myself worked. it created Main.class. so why doesn't it run?
2 years ago

Stephan van Hulst wrote:Yes. But CMD has a "current working directory". It's the currently selected directory, and it's shown to you by the command prompt. The current working directory matters to the behavior of the commands you execute.



So how am I supposed to change the directory? Sorry for being so stupid, but I fight with it since the morning and it's driving me crazy.
2 years ago

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:



I put the first in cmd and received: "javac: file not found: src\com\company\Main.java
Usage: javac <options> <source files>
use -help for a list of possible options"
2 years ago

Stephan van Hulst wrote:Try the following. From the project3 directory, run the following commands:



What do you mean by "from the project3 directory"? I thought I could run the program from cmd.
2 years ago

Stephan van Hulst wrote:Your directory structure suggests that your main class is part of a package called com.company, yet the source you posted doesn't contain a package statement.

You haven't told us the working directory from which you are running the commands.

You didn't show us how you call the java command.



Sorry, I am new. I don't know what is the working directory and what do you mean by how I call the command?
2 years ago