I have two questions :

  1. What is the difference between executing sh filename.sh and filename.sh?
  2. How can I make both of them giving me the same output ?

I'm asking this question as right now I'm facing a problem. I'm trying to run a Java + SWT application from terminal.

When I do filename.sh, it gives me the desired output. But when I do sh filename.sh or bash filename.sh, it throws me an error :

Exception in thread "main" java.lang.NoClassDefFoundError: MainForm/java
Caused by: java.lang.ClassNotFoundException: MainForm.java
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: MainForm.java.  Program will exit.

I know this question is already asked here but I'm still not clear about it.

I have gone through the following links :

What is the difference between ./ and sh to run a script?

Can scripts run even when they are not set as executable?

Can anyone help me with this?

  • If the script is not too long could you maybe post it? Pastebin is also ok – Gerhard Burger Dec 7 '12 at 18:29

The script probably knows which shell it should be running in. The first line might be something like


If you run it with sh, the first line (a.k.a. shebang) is ignored. A different shell tries to run the script, but does not understand it - it is like running Java code in Pascal. If you run it with the right shell, it should behave identically:

/bin/bash script.sh
| improve this answer | |
  • Do you mean the 1st line of my filename.sh? – RAS Oct 16 '12 at 10:33
  • @RAS: Yes, exactly. – choroba Oct 16 '12 at 10:34
  • First line of my filename.sh is java -classpath .:jars – RAS Oct 16 '12 at 10:57
  • @RAS: Hm... what output do you get if you run sh filename.sh in a terminal? – choroba Oct 16 '12 at 10:58
  • Exception mentioned in the question. – RAS Oct 16 '12 at 11:10

By putting bash or sh before your file, you force to use this shell to execute your file. So it's better to use the first line of the file :


So every time you execute your file it will be with the same shell.

And for your second question, no, you need to make a script executable to run it :

chmod +x file.sh
| improve this answer | |
  • You mean giving permission for "Execute : Allow executing file as program" ? – RAS Oct 16 '12 at 11:49
  • I'm not familiar with GUI, I was talking about a command you have to type in Terminal. But it sounds like the same thing, so give that a try. – NorTicUs Oct 16 '12 at 11:57
  • I asked you this as I already have gone through this link : askubuntu.com/questions/122428/how-to-run-sh-file – RAS Oct 16 '12 at 12:02
  • That's it. So what's your point ? – NorTicUs Oct 16 '12 at 12:14
  • When I do sh filename.sh or bash filename.sh, it throws me an error. – RAS Oct 16 '12 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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