• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running a Java Text File in Command Prompt  RSS feed

 
nicole peka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to run a text file for the HelloWorld app from Appendix B in the Command Prompt.  This is my first time using cmd, I have been using only Netbeans.  I am having trouble displaying the output.  I can open the file in command prompt, so I believe the path is correct. I have been watching youtube for the last three hours and searched through topics on code ranch.  May I please have some advice? Below is a pasted segment of the command prompt screen.

______________________________________________
C:\Users\nicole\Documents>javac -version
javac 1.8.0_60

C:\Users\nicole\Documents>javac C:\Users\nicole\Documents\HelloWorld.java

C:\Users\nicole\Documents>java C:\Users\nicole\Documents\HelloWorld.java
Error: Could not find or load main class C:\Users\nicole\Documents\HelloWorld.java

C:\Users\nicole\Documents>java HelloWorld
Error: Could not find or load main class HelloWorld
_______________________________________________
I am really exited to get started and any help would be appreciated.
Thanks,
Nicole
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Users\nicole\Documents>java C:\Users\nicole\Documents\HelloWorld.java

Leave off the ".java" suffix.
 
nicole peka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried leaving the ".java" off (after compiling). An error occurred, I pasted it below.  I think it's compiling without error (javac is the compiler: since there is no error message). After I tried to run it (java + [space] + file path address) it  could not find or load main class.  After that I made sure the file path was correct, I opened the document using the command prompt C:\Users\nicole\Documents\HelloWorld.java , which opened successfully. I still can't figure out where I am going wrong. I also tried the command: java HelloWorld, among other variations.

C:\Users\nicole>javac -version
javac 1.8.0_60

C:\Users\nicole>javac C:\Users\nicole\Documents\HelloWorld.java

C:\Users\nicole>java C:\Users\nicole\Documents\HelloWorld
Error: Could not find or load main class C:\Users\nicole\Documents\HelloWorld

C:\Users\nicole>cd Documents

C:\Users\nicole\Documents>HelloWorld.java
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nicole peka wrote:I tried leaving the ".java" off (after compiling). An error occurred, I pasted it below.


It is more than leaving off of the ".java". The command line parameter is not the name of the file -- it is the full name (including package) of the class. So, specify the class name. And if you are not at the root directory for the classes, then you need to also use the -cp switch to specify the class path.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like you're assuming your program in HelloWorld.java is written correctly just because it compiles. There may still be problems with it.  Cut and paste the output when you type the command: type HelloWorld.java
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This command:
java C:\Users\nicole\Documents\HelloWorld

will never work.

The 'java' command expects a fully qualified class name, and 'C:\Users\nicole\Documents\HelloWorld' is not a class name.

it needs to be:
java HelloWorld

But that assumes HelloWorld is not in a package and you are in the directory containing the classpath root (in this case, Documents).
If you are not, then you need to add a '-cp' in there that points to the Documents directory so the 'java' command knows where to find the class file.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:The 'java' command expects a fully qualified class name, and 'C:\Users\nicole\Documents\HelloWorld' is not a class name.

it needs to be:
java HelloWorld

Sounds right to me. In that case, it needs to be done after successfully doing cd Documents
 
Dave Tolls
Ranch Foreman
Posts: 3067
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going to say "but they're already in Documents"...however, that was only in the original post, and in that one we have this:

C:\Users\nicole\Documents>java HelloWorld
Error: Could not find or load main class HelloWorld

which I missed the first time around.

I think that would imply a classpath issue?  A CLASSPATH environment variable in this case?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could just be that the source doesn't have a correct main method.  There could also be a package statement.  We'd need to see the source to know.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
nicole peka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the source code. There is no package.  This is my first attempt at an application from notepad. This is everything in the document (minus the underscored lines). I also tried java HelloWorld after compiling the cmd prompt snippet is below as well
____________________________________



____________________________________

C:\Users\nicole>javac -version
javac 1.8.0_60

C:\Users\nicole>javac C:\Users\nicole\Documents\HelloWorld.java

C:\Users\nicole>java HelloWorld
Error: Could not find or load main class HelloWorld

_____________________________________
I appreciate everyone's help.  This is the first forum I have posted in.  I feel like a very slow learner.  It seems extra slow because I am working on this in between work. I would just use Netbeans but I think it is important that I learn how to code from the command prompt.
-Nicole
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried changing directory to "Documents"? Like this...

C:\Users\nicole>cd documents

C:\Users\nicole\Documents>javac HelloWorld.java

C:\Users\nicole\Documents>java HelloWorld
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For future reference, please use "Code" tags when posting source code or error messages. Highlight the text in the editor window and then click the "Code"  button just above the editor window. I fixed it for you this time.
 
nicole peka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops.. I will use code tags from now on.  Yes I changed directory as well. I get the same error

______________________________________________

C:\Users\nicole>cd documents

C:\Users\nicole\Documents>javac C:\Users\nicole\Documents\HelloWorld.java

C:\Users\nicole\Documents>java HelloWorld
Error: Could not find or load main class HelloWorld

C:\Users\nicole\Documents>

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nicole peka wrote:Oops.. I will use code tags from now on.  Yes I changed directory as well. I get the same error

______________________________________________

C:\Users\nicole>cd documents

C:\Users\nicole\Documents>javac C:\Users\nicole\Documents\HelloWorld.java

C:\Users\nicole\Documents>java HelloWorld
Error: Could not find or load main class HelloWorld

C:\Users\nicole\Documents>


Not exactly what I had. Compare the 'javac' lines.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that doesn't work I think it's time to go to your Documents folder and delete:
HelloWorld.java
HelloWorld.class

And start over. Do not cut and paste anything into your .java file, that could lead to non-ASCII characters being introduced.

When you get back to compiling again, look closely at the time stamp of your HelloWorld.class file and make sure it matches the time you ran javac.

If you still get the same error, look closely at this line

 
nicole peka
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello World!!! Thank you everyone for your help! I am so exited to get started with chapter 1. Carey, thank you for your advice.  I deleted the file and started over.  In the first few hours of trouble shooting, I did cut and paste from Netbeans to compare the code for any errors. I deleted it after comparing but something must have been introduced during the process. It is also good to know that I can't cut and paste when using notepad/cmd.  I believe knowing how to code in notepad will make me a more meticulous coder and I am excited to improve my skills.  I have already learned so much in the last two days(much you-tubing and googling). Have a wonderful evening everyone!
-Nicole
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it to work.

You might try the program Notepad++ to do your command line programming.  you should be able to copy and paste from an IDE (like Netbeans) to a text editor (like Notepad++) without problems.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!