LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 10-28-2001, 05:10 PM   #1
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Rep: Reputation: 47
Java problem


I have been trying to get a simple java program working in Linux but I cant seem to get it working.

Its the simple Hello World program. I can get it to compile in JavaByte code but when running it nothing appears at the command prompt.

Here is the code:

class Hello
{

public static void main(String[] args )
{
System.out.print("Hello world!");
}
}

No error messages at all. I compile ( javac Hello.java), then run (java Hello) and nothing happens.

Is it me? I do have the sun jdk installed into /usr/local. Any thoughts?
 
Old 10-28-2001, 05:51 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i'm pretty sure i'm wrong but java MIGHT be cos youv'e used print not println. i know C doesn't print the lin until you insert a newline, but other than that i can't suggest anything. I would've tried it, but i've not got java installed at the moment...

ahh bobbins... yes i have.. no the code works fine at least, no problems there... tired... time for bed...

maybe the standard out isn't set up ...? try redirecting the output to a file.
 
Old 10-29-2001, 08:13 AM   #3
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Original Poster
Rep: Reputation: 47
I should be able to use print, well I can at uni. Will do what you suggested when I get back home. I need some sort of tutorial on setting up Java in Linux. Do you know of anywhere?

Thanks for the reply
 
Old 10-29-2001, 07:04 PM   #4
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Original Poster
Rep: Reputation: 47
The output recorded in the file is Hello World!

But this never appears at the prompt?
 
Old 10-30-2001, 05:22 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well, i'd have to presume there's somethign up with your streams somewhere along the line, but i'd have no idea how to fix it!

tried just running it in tty1 or something?
 
Old 10-30-2001, 01:19 PM   #6
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Original Poster
Rep: Reputation: 47
Problem solved. You were right about println, print didn't come up wiv anything, but println did. Also, print worked for tty1, but not how I had imagined it to.

Thanks for the help, now to many hours coding!
 
Old 11-05-2001, 09:41 AM   #7
Bert
Senior Member
 
Registered: Jul 2001
Location: 406292E 290755N
Distribution: GNU/Linux Slackware 8.1, Redhat 8.0, LFS 4.0
Posts: 1,004

Rep: Reputation: 46
And the reason is ...

The print() method on it's own does not print to the screen unless the System.out.flush() method is invoked afterwards. The println() method effectively does it for you, so this would also work:

class Hello
{

public static void main(String[] args )
{
System.out.print("Hello world!");
System.out.flush();
}
}
 
Old 11-05-2001, 10:21 AM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
well, i found just print DID work, does java flush everything before killing the VM then by default?
 
Old 11-05-2001, 06:10 PM   #9
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Original Poster
Rep: Reputation: 47
I still get the same problem with print.

The print works, but the bash prompt ends up right after it and after pressing any key the text disappears and the prompt takes its place!

Also, I want to use another class in my program, and was told to stick in the same directory to be able to access it. Is this correct as the compile doesn't work, saying it can't find the class.
 
Old 11-06-2001, 03:03 AM   #10
glj
Member
 
Registered: Jul 2001
Location: London
Distribution: RH 9
Posts: 151

Rep: Reputation: 30
Maybe a "\n" (newline) at the end of the print would clear the first problem up. As far as accessing another class, as with everything, Java is case sensitive, so make sure that you have specified the name exactly right. If you start compiling your first class, it should check the dependencies and compile the class it is including. To be sure you could compile the second class first, sort out any errors, and then compile your first class.
If your program splits into loads of classes though, you can trust the dependency checker to sort everything out
(the java compiler is ALWAYS right )

HTH

glj
 
Old 11-06-2001, 03:58 PM   #11
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Original Poster
Rep: Reputation: 47
It may have helped had I actually compiled the classes in the first place! They were in .java when I downloaded them. Doh!

Cheers for the advice.
 
Old 03-11-2004, 10:02 AM   #12
rb3ng
LQ Newbie
 
Registered: Mar 2004
Posts: 28

Rep: Reputation: 15
still on running java for the first time

now forgive me if i take u guys back ,but i am new to linux and its been pretty difficult coming to terms with it.now my problem is simlpe even if it sounds dull ,i am desperate to get java running on my redhat linux system .it was installed by someone else and i dont know where it is but it is installed .so how do i run it . the only text editor i have is emacs ,how do i use it to key in my programs or could i use pico.thirdly i downloaded an online version of thinking in java ,how do i unzip it.i am desperate and will really appreciate step by step elementary answers .please i await your accelerated response .i have read a lot of your posts and you guys have been magnificent, please help get me started.
 
  


Reply



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
Java Problem...again theguitarsquall Mandriva 8 01-08-2005 12:44 PM
Java Problem anirudh Programming 6 10-27-2004 03:42 PM
Java problem bigdog632 Linux - Newbie 5 02-18-2004 05:22 PM
Java problem please help bwaynej2002 Linux - Software 2 02-06-2004 08:38 PM
Java problem oicdn Linux - Software 10 01-08-2004 07:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration