LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-15-2001, 11:52 PM   #1
anoop_chandran
Member
 
Registered: Nov 2001
Distribution: Redhat 7.0 ,mandrake 8.0 ,Redhat 7.2
Posts: 99

Rep: Reputation: 15
How to convert a Java Program into....


hi,

i have written a java program and i am able to compile the program by

javac myprog.java

and then i can run the program by

java myprog

what i want to know is whether it is possible in linux to convert this class or the java program into an executable program ,so that i needn't type

java myprog

everytime i have to run the program..i mean for example
by typing

$./myprog

i should be able to run my program....
someone told me that in windOS by converting it to jar file it can be run without any command..i tried that in linux by converting it to a jar. but then it says it's an archive...:-)).

somebody pls help me...
 
Old 11-16-2001, 12:35 AM   #2
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
The latest version of gcc has the abaility to make native binaries from java (using gcj) but I haven't had much success with it.
 
Old 11-16-2001, 02:04 AM   #3
anoop_chandran
Member
 
Registered: Nov 2001
Distribution: Redhat 7.0 ,mandrake 8.0 ,Redhat 7.2
Posts: 99

Original Poster
Rep: Reputation: 15
tell me where i can get gcj ,?

thanx.
 
Old 11-16-2001, 02:32 AM   #4
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Its part of the latest gcc... which is at gnu.org
 
Old 11-19-2001, 08:06 PM   #5
tyler_durden
Member
 
Registered: May 2001
Posts: 125

Rep: Reputation: 15
you MUST run the java virtual machine when running the java program, but there are a few things you can do to make an executable.

first, just make a script file in it, put java filename.java then make the scirpt executable, this will do the trick for you in linux. also, if you are really bored, you can make a c progam that just runs a system call to run the file. i would look like

#include <stdlib.h>

int main(){

system("java filename.java");
return 1;
}

just compile this (using gcc -o COMMANDNAME file.c)
where the commandname is the command you want to call (basically the executable) and the file.c is the name of the file.
this will only work if the java path is set up right on the machine.

for winblows, you can create a .bat file that just has java filename.java in it. you MAY be able to to run the above c program too, but i am not so sure about the win api.

if you want to create a jar file, in windows, just zip the file and rename it to jar, its the same thing. then you can double click it and it will run, but you have to name the jar the same as the class with the main thread.

Last edited by tyler_durden; 11-19-2001 at 08:09 PM.
 
Old 11-20-2001, 01:24 AM   #6
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Quote:
Originally posted by tyler_durden
you MUST run the java virtual machine when running the java program
Not quite true. Native compilers do exist. You only need the JVM to run bytecode.
 
Old 11-22-2001, 11:43 PM   #7
anoop_chandran
Member
 
Registered: Nov 2001
Distribution: Redhat 7.0 ,mandrake 8.0 ,Redhat 7.2
Posts: 99

Original Poster
Rep: Reputation: 15
let's froget windOS........
so if i use this C pgm to run the java pgm won't the $CLASSPATH make problems.......!

thanx
 
Old 11-23-2001, 02:42 AM   #8
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Quote:
Originally posted by anoop_chandran
let's froget windOS........
so if i use this C pgm to run the java pgm won't the $CLASSPATH make problems.......!

thanx
The C program was an "if you are really bored", you can just use a shell script.

/usr/local/bin/myprog:
Code:
#!/bin/sh
java /path/to/myprog
(where /path/to/myprog is the class file without the extension).

... and $CLASSPATH isn't an issue becuase it doesn't change just becuase you run a shell script (or C program) AFAIK.
 
Old 11-23-2001, 09:44 AM   #9
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
Ooh hang on, how about compiling the program and then typing:

%chmod x + g myprog.class

then you should be able to run it like:

%./myprog
 
Old 11-23-2001, 05:15 PM   #10
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Quote:
Originally posted by Bert
Ooh hang on, how about compiling the program and then typing:

%chmod x + g myprog.class

then you should be able to run it like:

%./myprog
No, Java class files are bytecode and not ELF or a.out executables. Linux can't just execute them.
 
Old 11-26-2001, 10:01 AM   #11
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
Ah.

Yeah, sorry, it's only applets that can use that.
 
Old 11-26-2001, 10:16 AM   #12
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Re: Ah.

Quote:
Originally posted by Bert
Yeah, sorry, it's only applets that can use that.
Ummm... no.

Applets are even tricker to get going then applications.

You need to create a webpage with the applet in it, then load the webpage in a java capable web browser or appletviewer.
 
Old 11-26-2001, 04:25 PM   #13
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
Ummm, yes.

Dorwardi, with all respect:

$javac MyStandaloneWhatever.java
$chmod +x MyStandaloneWhatever.class
$./StandaloneWhatever.class

will work unless you have have less than kernel version 2.0

It's always a good idea to try these things out first

You're forgiven for thinking it might not work because according to 'Running Linux' by O'Reilly, IBM and Microsoft can't figure it out either.
 
Old 11-26-2001, 04:41 PM   #14
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Re: Ummm, yes.

Quote:
Originally posted by Bert
Dorwardi, with all respect:

$javac MyStandaloneWhatever.java
$chmod +x MyStandaloneWhatever.class
$./StandaloneWhatever.class

will work unless you have have less than kernel version 2.0

It's always a good idea to try these things out first

You're forgiven for thinking it might not work because according to 'Running Linux' by O'Reilly, IBM and Microsoft can't figure it out either.
david@cyberman:Assignment5]javac in2post.java
david@cyberman:Assignment5]chmod +x in2post.class
david@cyberman:Assignment5]./in2post.class
bash: ./in2post.class: cannot execute binary file
david@cyberman:Assignment5]uname -a
Linux cyberman 2.4.15-greased-turkey #1 Sat Nov 24 19:20:45 GMT 2001 i686 unknown
david@cyberman:Assignment5]
 
Old 11-26-2001, 06:29 PM   #15
CragStar
Senior Member
 
Registered: Oct 2000
Location: UK - Frome
Distribution: Ubuntu
Posts: 1,081

Rep: Reputation: 47
Not being a java expert, far from it, I didn't think you could simply execute a class file from the shell. I always thought that you need the interpreter.

Just thought I'd add.
 
  


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
Program to Convert ogg to mp3 degraffenried13 Linux - General 3 06-27-2005 07:15 PM
Convert Java to C++? leanieleanz Programming 6 03-04-2005 12:14 PM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
can't convert o to FLoat in Java alaios Programming 4 04-24-2004 02:10 PM
Convert program huno Linux - Software 1 02-21-2003 04:30 AM

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

All times are GMT -5. The time now is 04: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