• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Classpath not set correctly

 
Ranch Hand
Posts: 42
2
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My best guess is that I haven't set the CLASSPATH environment variable correctly. I can run the HelloWorldApp from the oracle documentation from eclipse (i changed the name of the class name to avoid conflicts):

HelloWorldApp

I am using Ubunutu 16.04, and I'm trying to create the same HelloWorldApp from the command line to practice coding from the console using vim. I typed the following on the cli from inside the folder where the HellowWorldApp java files are located:

javac HelloWorldApp.java
java HelloWorldApp

Then I get the following error message:
Error: Could not find or load main class HelloWorldApp

But, when I type the following I get the correct output:
java -cp . HelloWorldApp

When I enter echo $CLASSPATH I get the following:
/usr/lib/jvm/java-8-oracle/lib

I don't think I made any changes to PATH or CLASSPATH in the ~/.bashrc



This is where I set my environment variables: sudo vi /etc/environment



Does anyone have any suggestions?
 
Marshal
Posts: 78441
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably shouldn't edit the /etc/environment file. Edit ~/.bashrc instead, assuming you are using the Bash shell, which is the commonest on Ubuntu. You don't need root privileges to edit .bashrc.
You are probably best removing that CLASSPATH entry altogether. You don't need a system CLASSPATH at all, and as you can see, it tends to do more harm than good. You would probably do well to move the JAVA_HOME and WILDFLY_HOME variables into .bashrc, too.

Campbell's terminal (bash) wrote:echo $CLASSPATH

[edit]Now I can see that quote isn't clear. There is a blank, empty line at the bottom of it.
 
Brian Jones Jr.
Ranch Hand
Posts: 42
2
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell,

Thanks for your advice. I checked to see if I am using bash by typing in ps at the command line, and it is running bash.

I updated the ~/.bashrc and the /usr/environment files as you recommended. Now my .bashrc file looks like this:



I removed the environment variables in the /usr/environment file and then ran unset $CLASSPATH because I still got the same error message "Error: Could not find or load main class HelloWorldApp" when I tried to run the program. Then I saw an empty line when I execute echo $CLASSPATH. So I tried to run the program again, and I still get the same error message.

After that, I removed the environment variables that I added to the .bashrc file, and investigated other ways to set the environment variables. I tried using these explanations on how to set environment varaibles, but I still get the same error:
https://help.ubuntu.com/community/EnvironmentVariables#Setting_values_to_environment_variables
https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps
http://www.linuxforums.org/forum/newbie/4325-how-add-environment-variable-bashrc-file.html

I moved the environment variables to the bottom of the page, and added some export commands. Currently my .bashrc file looks like this:



Do you know what I can try next?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What files are in the directory you are trying to run that HelloWorldApp?
Can you list them here?

What command(s) are you running?
Can you copy/paste them here as well, along with the output?

That should help us figure out what's going on.
 
Campbell Ritchie
Marshal
Posts: 78441
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In .bashrc, it shou‍ld read
export PATH=/usr/bin:etc
Don't simply copy the PATH you have in /etc/environment. You don't need the same PATH twice. You shou‍ld probably miss out the quote marks in the PATH and XYZ_HOME variables, and they need export too.I have commented out the PATH with a # sign and shortened it for reasons explained here.
You can change your PATH temporarily by writing a similar export instruction at a terminal. If that directory actually existed, I could use Java9 for the lifetime of the current terminal with this instruction:-Because Java9 precedes Java8 in the new PATH, it will take precedence over Java8.

Have you got the problem about Hello World sorted out?
 
Campbell Ritchie
Marshal
Posts: 78441
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave Tolls is right: tell us which directory you are in (pwd) and whether those files are in the same location (ls). If there are both HelloWorldApp.java and HelloWorldApp.class in your current location, show us the entire text of the .java file.
 
Brian Jones Jr.
Ranch Hand
Posts: 42
2
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:In .bashrc, it shou‍ld read
export PATH=/usr/bin:etc



Ok, so my export statements look like this.



Campbell Ritchie wrote:
Don't simply copy the PATH you have in /etc/environment. You don't need the same PATH twice.



I removed the PATH variable definition from /etc/environment, and moved it to .bashrc. Should the PATH variable definition be in /etc/environment or in .bashrc?

Campbell Ritchie wrote:
You shou‍ld probably miss out the quote marks in the PATH and XYZ_HOME variables, and they need export too.



I removed the quote marks from all of the environment variables, and added export too.

Campbell Ritchie wrote:
I have commented out the PATH with a # sign and shortened it for reasons explained here.
You can change your PATH temporarily by writing a similar export instruction at a terminal. If that directory actually existed, I could use Java9 for the lifetime of the current terminal with this instruction:-Because Java9 precedes Java8 in the new PATH, it will take precedence over Java8.



I don't know too much about the PATH variable. I only know that I need to set it when I want to install JDK, and I just read a little about it on wikipedia. Do you think that this might be part of the problem? Can I leave my PATH variable like it is for now, and get back to this later? I rather do more research on this later, and keep it separate from this post.

Campbell Ritchie wrote:
Have you got the problem about Hello World sorted out?


No, not yet.
 
Brian Jones Jr.
Ranch Hand
Posts: 42
2
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Dave Tolls is right: tell us which directory you are in (pwd) and whether those files are in the same location (ls)


Dave Tolls wrote:
What files are in the directory you are trying to run that HelloWorldApp? Can you list them here?



These files are in the directory where I am trying to run the HelloWorldApp:
HelloWorldApp.java
HelloWorldApp.class

I used pwd from inside the folder containing these files:
/home/bbernales/workspace/HelloWorldApp

Dave Tolls wrote:
What command(s) are you running? Can you copy/paste them here as well, along with the output?



From /home/bbernales/workspace/HelloWorldApp I run the following commands and I received this output:
bbernales@ubuntu:~/workspace/HelloWorldApp$ java HelloWorldApp
Error: Could not find or load main class HelloWorldApp

I think I should include this. When I type the following, the code runs correctly:
$ java -cp . HelloWorldApp
Hello World!

Campbell Ritchie wrote:
If there are both HelloWorldApp.java and HelloWorldApp.class in your current location, show us the entire text of the .java file.




 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this works:
java -cp . HelloWorldApp

that means you have a CLASSPATH defined that is overriding the default that the java command uses.

That is the only issue now.

Your PATH is fine, since you can use the javac and java commands.
So something, somewhere is setting CLASSPATH.
 
Brian Jones Jr.
Ranch Hand
Posts: 42
2
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I have commented out the PATH with a # sign and shortened it for reasons explained here.?



Ok, I just read the post. From what I understand, I should remove unnecessary details from the code. I'll make the adjustments. Thanks.
 
Brian Jones Jr.
Ranch Hand
Posts: 42
2
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave and Campbell! I will look for where this CLASSPATH variable is being set, and I'll remove it.
 
Campbell Ritchie
Marshal
Posts: 78441
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian Jones Jr. wrote:. . . Ok, so my export statements look like this.

Remove the top three lines, which only appear to be duplicates of lines 4‑6.

. . . . Should the PATH variable definition be in /etc/environment or in .bashrc?

The general PATH should be in /etc/environment. Only use .bashrc for something you installed for your own use. Otherwise any other users can't use any programs from the terminal. I think you shou‍ld restore the PATH in /etc/environment. That doesn't appear to need export.

. . . I don't know too much about the PATH variable. . . .

It is an important resource for the OS (=operating system). Whenever there is an instruction to run a program, the OS goes through the PATH looking for something with the same name.

Do you think that this might be part of the problem? Can I leave my PATH variable like it is for now, and get back to this later? . . .

No, nonononononono, no, no no, spelt y‑e‑s. I think you ought to restore the PATH to its original location, but if you are getting your terminal to run, you can get away with continuing like that for a little.
How did you install Java®? If it has settled in /usr/lib/... you probably used sudo for installation. Did you use the special technique for Ubuntu linked to from here or the .rpm installer? If you move the PATH back to /etc/environment you probably don't need a PATH in .bashrc.

If java still can't find the Hello World program, and ls has no problem finding it, that looks like a CLASSPATH problem as DT said. The code you posted looks all right. Try running it with
java -cp . HelloWorldApp
There is a contradiction between your seeing an empty line for CLASSPATH and your failing to execute the Hello World app. DT is right that something is setting a CLASSPATH. Try unset CLASSPATH without the dollar sign.
In a fit of desperation I shall suggest a couple of things, but we really are scraping the bottom of the barrel here:-
  • 1: cd / then sudo grep -R "CLASSPATH=" * which shou‍ld find any files where you have set a CLASSPATH and forgotten.
  • 2: export CLASSPATH=~/Documents:$CLASSPATH:~/Documents then echo $CLASSPATH
    That will really mess up your CLASSPATH, but if you set it at the terminal, it won't affect other terminals. See whether you have somehow got the CLASSPATH set to whitespace only.
  •  
    Campbell Ritchie
    Marshal
    Posts: 78441
    374
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can read about environment variables in the Java™ Tutorials.
     
    Brian Jones Jr.
    Ranch Hand
    Posts: 42
    2
    Mac Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:
    Remove the top three lines, which only appear to be duplicates of lines 4‑6.



    OK, I made the changes.

    Campbell Ritchie wrote:
    I think you shou‍ld restore the PATH in /etc/environment.



    I restored the PATH in /etc/environment. Removing the PATH from there ruined my OS. I had to restore from a backup lol.

    Campbell Ritchie wrote:
    How did you install Java®?



    I was trying to learn how to install Java from command line. I think it would be helpful for use with Docker. I think I used this website:
    Java CLI installation

    Campbell Ritchie wrote:
    In a fit of desperation I shall suggest a couple of things, but we really are scraping the bottom of the barrel here:-

  • 1: cd / then sudo grep -R "CLASSPATH=" * which shou‍ld find any files where you have set a CLASSPATH and forgotten.
  • 2: export CLASSPATH=~/Documents:$CLASSPATH:~/Documents then echo $CLASSPATH
    That will really mess up your CLASSPATH, but if you set it at the terminal, it won't affect other terminals. See whether you have somehow got the CLASSPATH set to whitespace only.



  • I did the unset CLASSPATH. Now I don't see a CLASSPATH when I do an echo $CLASSPATH. The CLASSPATH doesn't get set when I close and open a new terminal. My best guess is that reinstalling the old image fixed things. I must have done something funky in the last week when I tried to set the CLASSPATH.

    Thanks Campbell and Dave. I learned a lot!

     
    Campbell Ritchie
    Marshal
    Posts: 78441
    374
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Brian Jones Jr. wrote:. . . I was trying to learn how to install Java from command line. I think it would be helpful for use with Docker. I think I used this website:
    Java CLI installation . . .

    Our FAQ includes a link to webupd8team which seems to give instructions about the same product, but simpler than what you quoted.

    What I do is to create a folder in /usr (root privileges) called java, then change its ownership
    ...usr$ sudo chown critchie java
    and unzip the latest .tar.gz from Oracle into it. That means I need a PATH set:
    export PATH=/usr/java/jdk1.8.0_121/bin:$PATH
    which of course goes in .bashrc.

    Thanks Campbell and Dave. I learned a lot!

    Our pleasure

    Have a cow for tenacity and high‑quality informative answers
     
    The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
    Low Tech Laboratory
    https://www.kickstarter.com/projects/paulwheaton/low-tech-0
    reply
      Bookmark Topic Watch Topic
    • New Topic