2

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?

RAS
  • 121
  • 7

2 Answers2

3

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

#!/bin/bash

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
choroba
  • 9,273
  • 1
  • 30
  • 42
1

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 :

#!/path/to/shell

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
NorTicUs
  • 2,362
  • 2
  • 18
  • 34
  • 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 : http://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
  • If you're referring to the error from the question, it's a Java error. Most likely, you're missing the classpath so it didn't find the main Java class – NorTicUs Oct 16 '12 at 12:29
  • But then why it runs successfully when I do `filename.sh` only? – RAS Oct 16 '12 at 13:07
  • let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/6141/discussion-between-norticus-and-ras) – NorTicUs Oct 16 '12 at 13:14