Running java in Bash script

Hi all,
I need to run a bash script in Linux which will then run a java program. I managed to run the script successfully (and running the java program eventually) by doing ./myScript. However, when my script is being called by some other programs, the java program doesn't run. I've check the classpath and path, seems everything is in place. I tried having this :
java myjavaProg
in myScript, it doesn't work.
I also tried this, it doesn't work as well :
java -classpath /usr/local/nagios/libexec myjavaProg
Anyone has any idea why? Thanks in advance.

I'd suggest editing your bash script, replacing the java call with one that invokes a class with a clearer sense of what it's doing. I mean, write a small class that creates a file in /tmp or something, so you can be more sure whether or not it's being invoked.
Perhaps your java program is running, but it's behaving differently in these circumstances.

Similar Messages

  • HOW DO I  RUN A UNIX BASH SCRIPT FROM JAVA??

    HI. Here's a tricky little problem i have. There's a unix bash script that has some commands in it, that manipulate a file. It appends a certain string variable to a file called users. The users file is an ordinary text file.
    I know this script to work perfectly, when i invoke it like this directyl from the command line: ./addusers.sh
    or even: bash /downloads/selinux/policy/addusers.sh
    Now, i have a java program, and its meant to just execute that script. It doesnt throw any Exceptions at runtime. But when i look at the users file, and expect it to have an extra line that was the string variable, the file is UNTOUCHED!
    Again, direct command line invocation works, but not from java. Here's what my invocation from java looks like:
    Process p = Runtime.getRunTime().exec("bash downloads/selinux/policy/addusers.sh");
    The strange thing is, i tried a different bash command. I tried:
    Process p = Runtime.getRunTime().exec("mkdir /temporary");
    and this worked!
    so why not the other one??
    I cant figure it out.

    You say:
    bash /downloads/selinux/policy/addusers.sh
    And you say in Java:
    Process p = Runtime.getRunTime().exec("bash
    downloads/selinux/policy/addusers.sh");
    As if a leading / would be missing from the Java
    version...nyix says:>
    ...OK sorry about that. i DO have a / in front of the downloads.... section in the java method. So its:
    Process p = Runtime.getRunTime().
    exec("bash /downloads/selinux/policy/addusers.sh");
    HELP please?

  • Solaris 11 - run a simple BASH script on computer startup

    I need to have a simple BASH script run on my Solaris 11 machine automatically whenever the computer (re)starts. It should be run with root permissions and after the computer has fully booted. What is the easiest way to do that?
    Thank you
    Dusan

    Hi user9368043
    Yes, that should be right, and be intended this way.
    See /etc/rc3.d/README and the following part from smf(5):
    Legacy Startup Scripts
    Startup programs in the /etc/rc?.d directories are executed
    as part of the corresponding run-level milestone:
    /etc/rcS.d milestone/single-user:default
    /etc/rc2.d milestone/multi-user:default
    /etc/rc3.d milestone/multi-user-server:default
    Your question concerning upgrading to Solaris 11.1:
    In the Gnome menus, you should look for (and start)
    System --> Administration --> Update Manager
    Let it do its work. It will give you a new boot environment, containing Solaris 11.1. Possibly, you have to perform upgrading twice. With "beadm activate", see beadm(1M), you can go back to Solaris 11.0 whenever you want.
    "Local" parts of your zfs root pool, like /usr/local, home directories, /root, and so on, should be in separated file systems, and be mounted outside the root pool before upgrading. They are availlable then from any boot environment, and will not be duplicated. See more in zfs(1M), zpool(1M).
    I strongly recommend upgrading. Solaris 11.1 is great.

  • Why does do shell work differntly from running the same bash script in terminal?

    i have the following a/s:
    set scriptPath to quoted form of ((POSIX path of (path to me)) & lnThumb)       set ftn to ""       repeat            try                 do shell script scriptPath & " " & ppath & " " & ftn                 exit repeat            on error error_message number error_number                  if error_number is not -128 then                      set ftn to (choose file with prompt "select folder thumbnail" default location dir without invisibles)                      set ftn to quoted form of the POSIX path of ftn                 end if            end try       end repeat
    calling this bash script:
    #!/bin/bash -f   cd "$1"  rm ./folderthumb.jpg   if [ $# -gt 1 ]  then      ft=${2/#"$1"\//}  else      ft=$(awk '/folderIcon=/{printf("%s",$1)}' meta.properties  | sed s/folderIcon=// )      if [ 0$ft -eq 0 ]       then          exit 1      fi      ft="thumbs/"$ft  fi  ln -s $ft ./folderthumb.jpg  exit 0
    which works as expected when run from the cli:
    tomw> /Applications/imageProc/fixJAlbum.app/Contents/Resources/Scripts/lnThumb.sh `pwd` `pwd`/thumbs/P1040161.JPG   tomw> ls -l folderthumb.jpg lrwxr-xr-x  1 tomw  admin  19 Oct 22 10:36 folderthumb.jpg -> thumbs/P1040161.JPG
    but fails to strip the common directory when called from do shell:
    tomw> ls -l folderthumb.jpg   lrwxr-xr-x  1 tomw  admin  89 Oct 22 10:36 folderthumb.jpg -> /Users/tomw/Desktop/trip2albequerque/albequerque/oldRailyards2nobHill/thumbs/P1 040133.JPG
    w-t-f???

    Click on the "Use Advanced editor" on the upper Right of the edit box.
    Select the text you want preserved
    Click on the >> icon then select "Syntax Highlighting" -> "Plain"
    And you will get this
       kind of
    output    when    you     post

  • [Systemd] - How to tell if a service is running in a bash script

    I need a bash script to query systemd (systemctl or systemd or?) to see if a given service is running. Is there a way to do it talking to systemd directly, not by looking for a pid file or seeing if a process is running? Thank you.

    Good point.
    I'd say that, in the case of "RemainAfterExit=yes" services, it's not really black and white. The effects of the service are active, even if the service itself has exited, but it's up to the user to decide whether or not that's "running" in their opinion.

  • Running java in linux script

    Suppose to package all necessary *.class in example.jar in following way,
    bin.start.class
    business.bus.class
    and the class file that contain main is bin.start.class.
    There is no problem to run following under linux shell prompt. java VM is under PATH searching path. i.e.
    $java -classpath example.jar bin.start
    However, there is some problem to run a script which contains 'java -classpath example.jar bin.start' under linux shell prompt.
    May I have any clue to identify this problem?
    Thanks in advance.

    For case 1: Does any case work? When I see "[un]able to locate the main method" then I think that that the class was found but that the main method is not declared properly.
    Of course, main must be declared with exactly these keywords:
    static public void main( String arg[]) {
    ...For case 2: you still have some kind of classpath problem.
    If your script really has the line
    java -classpath example.jar bin.start
    then I still think your current working directory at the time that this line is run not that same directory as where the jar file is.
    If you have JAVA_HOME defined, you might want to try:
    pwd
    $JAVA_HOME/bin/java -classpath example.jar bin.start
    Another suggestion: If your $PATH variable is long, try to shorten it to a short list of directories. You can test with just /bin:/usr/bin .
    Also, is your shell Bash or C-shell? Is your script Bourne shell (/bin/sh) or Bourne?

  • Running Java program from script shell using SSH

    Hello!
    I have this problem: I need to run a java command like: java Prog arg1 arg2 arg3 on i machines, using a shell script.
    First of all, I'm trying to make it work for a single machine. But the problem is when I write the line << ssh user@localhost "java -classpath /a/b/ Prog arg1 arg2 arg3" >> and run the script, it is not able to execute the entire line. Is there a special options for the arguments of my Prog class? I want to mention that between my arguments there is another text file which is read by the Prog and when i run the script says it cannot find that file; Please any ideas for not remain stuck here?
    Thank You in advance!

    Hello! Thank you for being so helpful..
    The part with << is not existent in my script, I've put it just here for quotation.Sorry
    So, from the terminal under Linux I run the command like this: I go to my folder where I have all my .java Classes and I execute the command java Prog arg1 arg2 arg3 . Where arg1...arg3 are arguments which are read from the terminal and then used in my Prog. One of the arguments is a text file, from which Prog.java reads line by line it's content. How can I run the same thing , under a script using SSH? This is basically my question, taking into consideration what I've written in my first post.

  • How to send 2 variable value from bash script into java.class

    #!/bin/bash
      a=10
      b=20
       echo $a $b | java addition
    donehi there,
    currently i have a simple java coding ( a + b ). and i m trying to connect with bash script but this bash script coudln't Enter 2nd value (b=20) while i running for it. may i know how do i can Enter 2 value into it?
    output from terminal
    [seng@localhost java_class]$ bash addition.sh
    =======================================================================
    simulation 1
    Num_a       = 10
    Num_b       = 20
    Enter your Num_a : 10
    Enter your Num_b : Exception in thread "main" java.lang.NumberFormatException
       at java.lang.Integer.parseInt(java.lang.String, int, boolean) (/usr/lib/libgcj.so.6.0.0)
       at java.lang.Integer.parseInt(java.lang.String) (/usr/lib/libgcj.so.6.0.0)
       at filter_god.GOD(java.util.List, java.util.List, java.lang.String, java.lang.String, int) (Unknown Source)
       at filter_god.main(java.lang.String[]) (Unknown Source)
       at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0)
       at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)
    =======================================================================

    That code will send both numbers on a single line in standard input to the java process. So if the process reads standard input, it will get a single line that has this in it: "10 20".
    I'm guessing you're sending that whole line to Integer.parseInt. But a valid number doesn't have a space in the middle.
    You should split up the line using String.split. Or use a StringTokenizer. Or a regular expression. Or you can use a java.util.Scanner. Or a java.io.StreamTokenizer. Or maybe some other stuff that has slipped my mind at the moment.

  • Running Bash scripts from jsf

    Hey I was just wondering if anyone knows how to run a bash script (whatever.sh) from a jsf page.?? I have a whatever.sh on a tomcat server where my web ap is and I was trying just to run it? Any idea???

    Why do you want to do this operation in a JSF page?
    It is better to do it in a Java class and then call the class from the JSF/ JSP page - keep JSF/ JSP only for disply / view purposes.
    Searc on Google for "How to call unix shell script using Java" for answers.

  • Java script and the new and not improved firefox don't work. Please let me be able to run Java again.

    I use SAP Infoview for work. To modify reports it requires a Java script to run. I had it set up in the previous version of ff to always allow from this specific site and it worked wonderfully. Now, in the new version, despite following all of the instructions from others, I cannot do my work because I cannot run Java script. I can still run it in Safari, but Safari is a last resort for me to use. I prefer Firefox and have all of my links stored there.

    Some sites are not fully compatible with Firefox 4's new HTML5 rendering engine. Can you turn that off and test without it? Here are the steps:
    (1) Open a new tab, then type or paste '''about:config''' in the address bar and press Enter. Click the button promising to be careful.
    (2) In the filter box, type or paste '''html5''' and pause while the list is filtered
    (3) Double-click '''html5.parser.enable''' to toggle it to false (it should turn bold).
    Then change back to the SAP tab and reload the page.
    Does that make any difference?

  • Exit status running java classpath in a unix shell script

    I'm new to putting java into unix scripts. I have a java classpath running inside of a unix shell script. During my testing it will error with java.io.FileNotFoundException error, which I know why that is, but when I set in my unix shell script this to see the right exit status of success/fail, it always shows a 0 for success when that isn't really the case. Below is the two lines I have set to capture the exit status and just display that exit status for now.
    notifycode=$?
    echo $notifycode
    I have these 2 lines above on a line right below my java command in my unix shell script. How can I get my unix shell script to show the right exit status if the java classpath command fails? Thanks for any help.

    That's Java code, it says "End this Java application and send return code 1 back to the shell". As for how the shell gets the return code from the application, that's a question about your shell and not about Java programming, no?

  • Bash script run via cron not executing MYSQL command

    I have a bash script which is run from a cron,
    It is executing as I have it write to a log file which it does correctly.
    I am wanting the bash script to restore a mysqldump file.
    When I run the bash script manually the dump file gets loaded fine. But when I run it through the cron the mysql command appears to be ignored.
    The mysqldump file is 54MB and I have checked to make sure that MYSQL is included in the global users path in /etc/profile
    Does anyone know why this maybe??
    Here is the bash file
    #!/bin/bash
    date >> /home/user/crons/crons.log
    echo "Started loadbackup" >> /home/user/crons/crons.log
    cd /home/user
    dbuser=root
    dbpass=password
    dbname=databasename
    filename=backup
    mysql -hlocalhost -u"$dbuser" -p"$dbpass" "$dbname" < " >> /home/user/crons/crons.log
    My crontab looks like
    02 17 * * * /home/user/crons/loadbackup.sh
    Many thanks
    Richard

    Hi Richard,
    Have you tried redirecting the script output in the cron to see if an error is being reported?
    I.e.
    02 17 * * * /home/user/crons/loadbackup.sh > /tmp/loadbackup.log 2>&1

  • Can you run a bash script on boot in single user mode

    Hey guys quick question.
    Is it possible to run a bash script on boot in single user mode.
    I can create a file and dump it on the root hd.
    Let's call it repair.
    I can then boot to single user mode and run it by typing /repair.
    But I want it to do it automatically.
    Every time I go into my machines that I clean for my job. I have to run sbin/fsck -fy
    Then I have to mount the drive and then remove all cache files, then reboot the machines.
    I would like to automate this by just holding command s and then moving to the next computer.
    There must be some sort of boot daemon somewhere.
    Please help.
    Sincerely,
    John

    Have you seen Applejack?
    http://applejack.sourceforge.net/
    It doesn't start automtically, but does cleanup.
    Robert

  • How do I run a bash script *before* login?

    I've got a bash script that opens an SSH tunnel that I'd like to run (as root) before any user logs in. How do I go about getting this script to run between boot and user login?
    Thanks.

    Post Unix queries to the Unix forum under OS X Technologies where those mavens frolic.

  • Bash script running on my MBP 10.7.4 but not on other Macs (all 10.7.4)

    A friend of mine wrote me this short script to run in terminal that downloads a particular file every n seconds from a password protectected FTP site. The script worked on his linux machine and on my Mac but not on my collegues Macs. I'm wondering if anybody knows reasons why it may be failing on other Macs.
    We changing the first line from: #!/bin/sh to #!/bin/bash
    and exceuting with
    user_prompt_$ bash ./download_stats.sh
    Another idea is to script cyberduck but it doesn't support applescript though may have a comand line interface (looking into that at present).
    There error output from one of the accounts it fails on follows the script listing.
    #!/bin/sh
    # =================================================================
    # Download XML_stats Bash Script
    # Filename:     download_stats.sh
    # Arguments:    input_filename  - file to be downloaded from remote server. Optional.
    #               poll_interval   - seconds delay between downloads. Optional (input_filename must be present)
    # Use:          Invoke from shell, e.g. ./download_stats.sh
    # Author:       Luke Neeson
    # Date Created: 2012-05-31
    # Comments:     This script will download a given file, input_filename, at the given interval, poll_interval.
    # Notes:
    # - .netrc is required in the current directory and must contain the logon details for the fox server. This could be installed in the user home directory, if so, delete the line marked below.
    # =================================================================
    # If the .netrc is in the directory of this script, the following must be enabled. If the user's home .netrc is to be used, comment out the following line:
    export HOME="`pwd`"
    export input_filename="the_file_I_want_as_default.xml"
    export poll_interval=10
    if [ ! -z "$1" ] ; then
      export input_filename="$1"
      if [ ! -z "$2" ] ; then
        export poll_interval="$2"
      fi
    fi
    while [ true ]
    do
      ftp access.foxsports.com.au << EOF 2>&1 | sed 's/.*AUTH GSSAPI.*//g' | sed 's/.*KERBEROS.*//g'
      get  "$input_filename"
      quit
    EOF
      sleep $poll_interval
    done
    ERRORS RETURNED:
    a-mbp:~ macca$
    a-mbp:~ macca$ cd ~/stats/download_rugby_stats/
    a-mbp:download_stats a$ sh download_rugby_stats.sh
    : command not founds.sh: line 16:
    : command not founds.sh: line 19:
    : command not founds.sh: line 22:
    download_stats.sh: line 39: syntax error: unexpected end of file
    a-mbp:download_stats a$
    Those are the blank lines and 39 is the last line of script.

    Thanks Bob, Luke mentioned which but didn't go into it.
    cat -vte download_rugby_stats.sh   ——>    Does this look good to you?
    # ================================================================= $
    # Download Rugby Stats Bash Script$
    #$
    # Filename:     download_rugby_stats.sh$
    # Arguments:    input_filename  - file to be downloaded from remote server. Optional.$
    #               poll_interval   - seconds delay between downloads. Optional (input_filename must be present)$
    # Use:          Invoke from shell, e.g. ./download_rugby_stats.sh$
    # Author:       Luke Neeson$
    # Date Created: 2012-05-31$
    # Comments:     This script will download a given file, input_filename, at the given interval, poll_interval. $
    # Notes: $
    # - .netrc is required in the current directory and must contain the logon details for the fox server. This could be installed in the user home directory, if so, delete the line marked below.$
    # ================================================================= $
    $
    # If the .netrc is in the directory of this script, the following must be enabled. If the user's home .netrc is to be used, comment out the following line:$
    export HOME="`pwd`"$
    $
    export input_filename="Rugby_IRB20120101_Client.xml"$
    export poll_interval=10$
    $
    if [ ! -z "$1" ] ; then$
      export input_filename="$1"$
      if [ ! -z "$2" ] ; then$
        export poll_interval="$2"$
      fi$
    fi$
    $
    while [ true ]$
    do $
      ftp access.foxsports.com.au << EOF 2>&1 | sed 's/.*AUTH GSSAPI.*//g' | sed 's/.*KERBEROS.*//g'$
      get  "$input_filename"$
      quit$
    EOF$
      sleep $poll_interval$
    done$
    $

Maybe you are looking for

  • MRP Type P1 and late firm schedule lines

    Hello All,      We are using MRP Type P1 which firms our schedule agreement schedule lines within the planning time fence (as desired).  However, sometimes the supplier is late on delivery and the firm schedule line goes into the past.      Is it pos

  • Title "shifts" up in frame after it's been rendered. Any idea why?

    Hi, I'm pressed against a deadline (aren't we all?) and need help fast. A designer created lower third titles in Photoshop and saved them as .psd files. Imported them into FCP and they work fine. When I key them over footage, they work fine and key i

  • Size of site and images

    I have just made my first website in iWeb v2.04 on a Mac Book Pro 17" OSX 10.6.4. The total size of the published site looks rather small on my screen. I have a 17 inch screen and I understand that the site needs to be set to dimensions that can be s

  • Network Manager SOLVED!

    So, I just installed Arch last night, and I'm really proud of myself for getting almost everything working on my first try. However, there is still one thing that I'm still having problems with, NetworkManager. I installed both it and the NetworkMana

  • Error when create bank account transfer

    Hi All, When I created bank account transfer, i got error message "oracle.jbo.RowValException: JBO-27012: Row validation method validate() failed for row with key oracle.jbo.Key[282 ] in PaymentTransactionsEO" Need some help Thanks FN