Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have java 8 installed on fedora 20. Any java program runs perfectly fine via eclipse but when I run the same program on terminal it reports an error saying 'could not load main class' . what could be the problem?

I wrote a simple helloworld program named hello.java and executed on eclipse. Then I opened terminal and moved to the folder containing the hello.class in eclipse workspace and typed ' java hello '. But it said 'error:could not load main class'.

Also as I said I have installed and I use oracle java 8.

share|improve this question
    
Welcome to Unix.SE! You need to give a little more detail: what command are you typing to start your program? Please edit your question to add the info. –  Stephen Kitt Jun 29 at 7:57
    
Could you please paste the code of your Hello.java file? –  binfalse Jun 29 at 8:32

2 Answers 2

You need to open it with java -jar Hello

share|improve this answer
    
It did not work. –  Aditya Gupta Jul 3 at 17:43
    
What's the error? –  nifker Jul 3 at 17:44
    
Error: unable to access jarfile Hello –  Aditya Gupta Jul 4 at 15:29
    
Have you given the file execute permission? –  nifker Jul 10 at 0:03

in terminal type java -version and observe the version that appear.

say, it is v1

in eclipse Window->Preferences->Java compiler, check version

say it is v2

To work v2 must be <= v1, if eclipse has newer java version you need one of two:

1) install newer java on linux 2) downgrade java compiler in eclipse

share|improve this answer
    
Java version at both the places is 1.8 . –  Aditya Gupta Jul 4 at 15:27
    
    
Yeah.... Found the solution. I had to use '-cp . ' option to include the current directory in class path. Surprisingly it java does not even look in the current working directory for .class files. I had to use ' java -cp . Hello' to run the class file. –  Aditya Gupta Jul 26 at 11:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.