Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

For some reason, I cannot run my Java code in command prompt

 
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I have been programming Java for a while now, but I've always only used IDE's such as eclipse.  
But, when I had to test Console.readPassword(), Eclipse wouldn't work. So I had to use the command prompt to test it.  
But after hours of searching, I still cannot find out why it tells me it cannot find the main class, when I explicitly point to it.  
Here is my file hierarchy and my command.
D:\Java projects\Irrelevant folder1
                        Irrelevant folder 2
                        JavaEms\bin\ems\[.class files. Including the Main.class]
                                      src\ems\[.Java files.]
                                      .classpath
                                      .project
                                      3 .txt's used by the app
And the command is : Java -classpath D:\Java_projects\JavaEms\bin ems.Main
And it tells me that it cannot find or load main class ems.Main.
Any help is appreciated.
 
Marshal
Posts: 77541
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having difficulty finding an error in that information you have posted. Please show us the package declaration in your Main class.

I never use Console. When it came out it was a great thing, but there is a serious problem with Console. If you export your app as an executable .jar, your users will probably open it with javaw rather than java at the command line/terminal, and there won't be a visible Console and System.console() will return null. So any attempt to use the console will cause an exceeption to be thrown.
 
Saloon Keeper
Posts: 14792
333
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe because you have an underscore in Java_projects, and that's not the name of the folder?
 
F Lucas
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. Thanks for the reply. I do not know how to quote you two, so I will reply in a list.
Campbell : Is a package declaration this? -> package ems;
Also, what would be a good alternative to console?
Stephan : Without an underscore, it ends up searching for a main class called projects\JavaEms\bin ems.Main
Because the command ends up getting separated on the space.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't just put an underscore in there because of that, as 'Java_projects' != 'Java projects'.

Put the classpath in quotes:
java -classpath "D:\Java projects\JavaEms\bin" ems.Main
 
Campbell Ritchie
Marshal
Posts: 77541
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

F Lucas wrote:. . . Campbell : Is a package declaration this? -> package ems;

Yes. And I missed the underscore: good catch, Stephan.

Also, what would be a good alternative to console?  . . ..

Consider a GUI with a password field. There is such a class but I have forgotten its correct name. Maybe JPasswordField.
 
F Lucas
Ranch Hand
Posts: 58
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Stephan and Dave, it now works. Thank you.
Also thanks to Campbell for the tip about not using console.
 
Campbell Ritchie
Marshal
Posts: 77541
372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a pleasure
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic