LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-27-2005, 07:10 AM   #1
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Rep: Reputation: 30
java and bash script problem


Well. I am trying to lauch java applications with scripts. In same package there is TestSuite class and Client_Main class. Both work if I start them manually. Weird thing is that only Client_Main works with script. Running the first ( TestSuite ) script complains that command isn't found so that would mean it can't find java. I launched both scripts many times with same terminal session and still only second script starts java program. Writing lauch string for a command line works in both cases. Only thing I can thing of is underscore in class name but that shouldn't affect right?

Not working script
Code:
#!/bin/bash

CURRENTPATH=`pwd`
echo "Howdy Howdy ho"
echo "CURRENTPATH=$CURRENTPATH"
echo "J2RE_HOME=$J2RE_HOME"
echo  "Next should work"
`java client/TestSuite`
Working script
Code:
#!/bin/bash

CURRENTPATH=`pwd`
echo "Howdy Howdy ho"
echo "CURRENTPATH=$CURRENTPATH"
echo "J2RE_HOME=$J2RE_HOME"
echo "Next should work"
`java client/Client_Main`
I am running 64-bit fedora core 3 with 2.6.12 kernel.

And I still repeat myself...Problem is in a script. Class files can be launched from same directory by writing
java client/TestSuite
and
java client/Client_Main
 
Old 11-27-2005, 07:33 AM   #2
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
Found out that java programs actually start in both cases.
TestSuite class starts and it has line
System.out.println("Starting up" );
as a first line in static main method.
Display with script is
Starting.
Console output stops there.
I changed Starting to Revving and it showed but still no up word. Starting TestSuite from commandline works without a problems.

So I think I have to somehow alter my scripts but how?
 
Old 11-27-2005, 01:03 PM   #3
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Do you end up with a core file in your current directory?
 
Old 11-27-2005, 03:09 PM   #4
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by eddiebaby1023
Do you end up with a core file in your current directory?
Nope.

Here is modified javacode
Code:
package client;
import java.lang.*;

public class TestSuite{

    public static void main(String[] args) {

        System.out.println("Single");

        System.out.println("Revving up");

        System.out.println("Starting up");

    }


}
Here is script output
Code:
Howdy Howdy ho
CURRENTPATH=/stuff/trixsource
J2RE_HOME=/boot/java/j2sdk1.4.2/jre
Next should work
./runtest.sh: line 8: Single: command not found
And here is java client/TestSuite output
Code:
Single
Revving up
Starting up
Any clues. I have to install Tomcat soon to get a valid script.
 
Old 11-27-2005, 04:00 PM   #5
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
Ok. Get runtest.sh working. Had to use line
exec java client/TestSuite

This unfortunately doesn't then work for the Client_Main. Client_Main is a swing application and it starts extended jframe class. So is there a common string to get them both up and running and why there are changes in a script needed between console java application and windowed java application? Both are executed under X-windowing system ( Xorg 6.8.1 )
 
Old 11-27-2005, 09:24 PM   #6
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I think the back quotes are your problem. Why do you need them? Have you tried taking them out?

EXAMPLE:
Code:
echo "I'm about to run my Java program..."
java mydir/MyClass
ANOTHER EXAMPLE:
Code:
1) mkdir mydir

2) vi mydir/MyClass.java =>

package mydir;

public class MyClass {
  public static void main (String[] args) {
    System.out.println ("Greetings from MyClass!");
  }
}

3) javac mydir/MyClass.java
    <= COMPILES OK...

4) java mydir/MyClass
Greetings from MyClass!
    <= RUNS OK, TOO.
          NO SURPRISES SO FAR...

5) `java mydir/MyClass`
-bash: Greetings: command not found
   <= ERROR: THE SHELL IS TRYING TO EXECUTE MY OUTPUT!

6) MYVAR=`java mydir/MyClass`
    echo $MYVAR
Greetings from MyClass!
   <= OK: WE USED THE BACK-QUOTES TO COPY MY PROGRAM'S
         OUTPUT INTO A SHELL VARIABLE...

Last edited by paulsm4; 11-27-2005 at 09:33 PM.
 
Old 11-28-2005, 02:56 AM   #7
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
paulsm4. Yuo are right for a console java program.
But unfortunately it doesn't work without backquotas for a graphical swing program.

That is the part I can't understand at the moment.

I want a starting script which works for both cases. IMO that should be possible.
Maeby putting output to variable will work for both cases but there should be a way without variables to start both programs.
 
Old 11-29-2005, 01:03 PM   #8
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
UPDATE. Using variable for storing console output works for both cases. That unfotunately is unacceptable because then I have to exit program before I could see any debugging information.

So is there a common solution for both java applications which would show console output immediately when something is written to console?
 
Old 11-29-2005, 01:37 PM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I'm not sure what's going on, so let me throw some things out there:

FACT: You do NOT need to use backquotes in a shell script UNLESS you want to redirect the program's output into a shell variable.
If you're using backquotes for any other reason, it sounds like there's a problem.

FACT: To run a GUI Java program under Windows, use "javaw.exe MYPROG".
To run a command-line Java program under Windows, use "java.exe MYPROG".

FACT: On Linux, it shouldn't matter. You can use the same "java MYPROG" command to run either a GUI or a command line program.

FACT: You do *NOT* usually need to call "exec" from a script: you should simply be able to invoke the command from within the script.

FACT: Different scripts (e.g. "bash" vs "sh", or cmd-line vs "cron") can have subtly different environment and permission settings.
 
Old 11-29-2005, 11:48 PM   #10
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by paulsm4
Hi -

I'm not sure what's going on, so let me throw some things out there:

FACT: You do NOT need to use backquotes in a shell script UNLESS you want to redirect the program's output into a shell variable.
If you're using backquotes for any other reason, it sounds like there's a problem.

FACT: To run a GUI Java program under Windows, use "javaw.exe MYPROG".
To run a command-line Java program under Windows, use "java.exe MYPROG".

FACT: On Linux, it shouldn't matter. You can use the same "java MYPROG" command to run either a GUI or a command line program.

FACT: You do *NOT* usually need to call "exec" from a script: you should simply be able to invoke the command from within the script.

FACT: Different scripts (e.g. "bash" vs "sh", or cmd-line vs "cron") can have subtly different environment and permission settings.
Ok. I made a simple GUI program and it worked with the script without backquotes. Then I changed the problematic application to simple console application ( removed all the methods except main ) which had only one System.out.println line to print something to console.

Still no go without backquotes in a script. Writing execution string in terminal works and also backquotes in a script.

You commented that you don't know what is going on and so I don't know either. So someone explain this to me.

Edit:Change one string to script.

Last edited by markelo; 11-29-2005 at 11:50 PM.
 
Old 11-30-2005, 12:08 AM   #11
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
FINAL UPDATE:
Removed all the comment lines from problematic application. Also write every newline again ( deleted by backspace and then pressing enter ). Saved compiled and SUCCESS.

Problematic application was edited in MS windows and then transferred to linux OS. So windows newline codes somehow messed things up. Strange thing was that file run quite normally with backquotes in a script but without backquotes it simple reported classfilenotfound.

Mysterious problem solved BUT NOT YET EXPLAINED. So someone who knows the roots of this strange behaviour I would like to know more about it.

Ok. Thanks for your time. Moving on now.
 
Old 11-30-2005, 10:53 AM   #12
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Actually, there's little doubt about "what was going on". I was politely trying to say that you were jumping to a lot of false conclusions and fruitlessly chasing a lot of red herrings, and tactfully trying to suggest alternatives.

I'm satisfied with the explanation "DOS CR/LF's messed things up".

But if you'd like to .zip up and e-mail me the (failing!) versions of your Linux shell script and Java source, I'd be happy to give you a more precise explanation.

Your .. PSM
 
Old 12-01-2005, 02:36 AM   #13
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by paulsm4
Actually, there's little doubt about "what was going on". I was politely trying to say that you were jumping to a lot of false conclusions and fruitlessly chasing a lot of red herrings, and tactfully trying to suggest alternatives.

Your .. PSM
You weren't harash but you weren't too politely either.
IMO my conclusions were valid considering the behaviour I was experiencing. Anyway I will now strip code for the minimum, make sure that it behaves erratic and email it to you.

My conclusion is that it is fault of javac-compiler. I use following j2sdk

java version "1.4.2-02"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.2-02)
Java HotSpot(TM) 64-Bit Server VM (build Blackdown-1.4.2-02, mixed mode)

That is java for x86_64. It has java plugin for 64-bit mozilla browser.
 
Old 12-01-2005, 04:17 AM   #14
markelo
Member
 
Registered: Mar 2004
Distribution: Red hat 9 -> 64bit fedora
Posts: 190

Original Poster
Rep: Reputation: 30
I can't seem to reproduce exact behaviour I was experiencing earlier.

One thing I found in my scripts though.
I made them from msdos file runclient.bat as it had a long package execution path in it.

so instead of java.exe I removed .exe and then added that bash thing on top of file and renamed file to end with sh.

Thus msdos CR/LF was left in the script and backquotas propably fixed part of the problem.

But I haven't been able to run my programs partially with any script anymore. That was the behaviour I was getting earlier. If I encounter this behaviour again you can be sure I post you PSM all the source, classes and scripts and you can then explain me what is going on.

Lesson learned. Be carefull when editing windows text files in linux system with gedit.

Edit: Added anymore.

Last edited by markelo; 12-01-2005 at 04:19 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with bash script. cheater1034 Programming 9 11-21-2005 10:29 PM
bash script problem GATTACA Programming 3 06-13-2005 09:59 AM
bash script problem cookie_ie Programming 3 03-23-2005 02:19 PM
Bash script problem kalleanka Programming 2 01-30-2004 11:59 AM
Problem with bash script cowardnewbie Programming 1 10-01-2001 06:53 AM


All times are GMT -5. The time now is 02:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration