Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How run a java program without bluej  RSS feed

 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
Im new to java programming and i have a silly question.

I made a program, compiled and ran it in bluej.
My question is, How can i send it to my friend (knows nothing about java..) so he can run it by simply double clicking.

Edit:
Someone from another forum said i have to make a gui to my program, Is that true? Because i dont really know how to do it and im gonna waste alotta time sittin on it...

Thanks in acvance
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have suffered from BlueJ myself and do not like it.
You need a main method which you can read about here. It should be short, very short. If you have a simple app with all classes in the unnamed package and you are using class Foo to contain the main() method, you open a command prompt/terminal and navigate to your directory and write
javac Foo.java
java Foo

More details in this FAQ.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arik Shalito wrote:. . . i have to make a gui to my program, Is that true? . . .
No.

The easiest way to send it across the Net is to create a .jar file. You need to add the name of the class with the main() method in the manifest file. That all sounds very complicated but there are instructions in the Java™ Tutorials. Look particularly at the section about setting the entry point.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you don't need to write a gui to be able to run a Java program by double clicking on it. In fact having a GUI or not having one makes no difference to how you run your program.

What you do need to do is package your class file(s) in a jar (with a suitable manifest file) and then your friend needs to have Java installed on his machine. Then when he double clicks the jar it will launch your program. BlueJ will probably have an option for building a runnable jar but I've never used BlueJ so can't advise on how you use it, alternatively you can do it manually from the command line.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Before i do anything, I wanna make sure theres nothing missing for that.

Again, excuses for the newbism...
and thanks for the welcoming
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never used BluJ, but use NetBeans all the time--at this point many of the IDE's have executable JAR options. Your program looks fine at a glance, as you have never said it does not run I will assume that it does. You need to make an executable JAR file. This is probably an option in BluJ, but be advised if you make an executable JAR and he has the JRE installed locally, he will run the file alright, but when the program is done, it will exit and close the console window immediately, so he will not be able to sit and look at the result of your program.

You can run the class file by itself "javaw Shelf" or possibly "javaw Shelf.class" this has to be done at a console window and javaw.exe has to be in the path statement of the local machine. Also you will need to be in the folder where your Shelf.class file is located.

You can do the same thing using an executable JAR, so the window will stay open you open a console window and type "javaw -jar Shelf.jar" provided your executable JAR file is called Shelf.jar or just double click on the JAR file from file browser and it's going, but like I said it will close the window as soon as your program is done.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javaw Shelf
Not:-
javaw Shelf.class
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create an executable jar file in BlueJ, open a project, File -> Create Jar File..., and select the main() method that launches the program. (At least, I believe this is how you do it.)
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks tony and campbell, I followed the tutorials
but still:

I have set the JAVA_HOME and PATH enviroment variables:
added "%JAVA_HOME%\bin;" to the system variable PATH


command prompt:
this seem to work


this didnt, and i have no idea why... X_x


Les Morgan, the fact that I wont be able to see the end results is something I didnt take in consideration, i will add a piece of code to it, Thanks
but thats something i could find out if id be able to run it :<

Knute Snortum, I created an executable jar through bluej,
it has Java's icon, but when I double click it, nothing happens

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Open up a command prompt and type:
set <ENTER>

Now check the output to see if the JAVA_HOME variable is pointing to "C:\Program Files\Java\jdk1.8.0_60" and PATH includes "C:\Program Files\Java\jdk1.8.0_60\bin"
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you have set your PATH wrongly. If you nagivate to the folder with javac.exe in, the OS will be able to find it easily. If you are in a different folder and your PATH is set wrongly, how can it find javac?
Please read this how‑to and post the result of your writing
echo %PATH%
at the command line.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony, 'the syntax of the command is incorect'

Ritchie,
echo %PATH%

echo %CLASSPATH%
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you don't have the environment variable JAVA_HOME set. Do you know how to set it?
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No sir, But i googled it and I wish its the last obstacle:
For Korn and bash shells, run the following commands:

export JAVA_HOME=jdk-install-dir

export PATH=$JAVA_HOME/bin:$PATH

For the bourne shell, run the following commands:

JAVA_HOME=jdk-install-dir

export JAVA_HOME

PATH=$JAVA_HOME/bin:$PATH

export PATH

For the C shell, run the following commands:

setenv JAVA_HOME jdk-install-dir

setenv PATH $JAVA_HOME/bin:$PATH

export PATH=$JAVA_HOME/bin:$PATH


I have no idea which shell I need.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're in windows so the shell is CMD.EXE. If you search for Command Prompt in the start menu, you should find it, or just type CMD.EXE in the space under the start menu (or in windows 10, in the search).

So you're at a command prompt. To set JAVA_HOME for just this session, type



You should be able to type



and get a version number. Now to set it permanently, type

 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . If you search for Command Prompt in the start menu . . .
Start Menu→All Programs→Accessories→Command Prompt

Sorry got it wrong first time and had to add Accessories.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cmd:
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setx JAVA_HOME %JAVA_HOME% will only work if you've previously done set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_60" in the same command window.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well yes, I did do what Knute Snortum told me:
So you're at a command prompt. To set JAVA_HOME for just this session, type

?
1
> set JAVA_HOME="C:\Program Files\Java\jdk1.8.0_60"


You should be able to type

?
1
> javac -version


and get a version number. Now to set it permanently, type

?
1
> setx JAVA_HOME %JAVA_HOME%


and still javac -version isnt working
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the 1 come from?
Did you set the JAVA_HOME variable before using setx?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this. From a command prompt, type


then

Do you get a Java version number? If not, then type

and post it.

If you do see a version number, type

Do you see C:\Program Files\Java\jdk1.8.0_60\bin; in there? If not, post the PATH.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a peculiar JAVA_HOME. Try
set PATH="C:\Program Files\Java\jdk1.8.0_60\bin";%PATH%
at the command prompt. That assumes you installed JDK8u60 in its default location and that you have C as your default drive letter. Then try
javac -v
again.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That suggests you have a correctly‑set PATH now. It should have been
javac -version
Sorry for that mistake.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for the headache,



 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth did you manage that? Have you closed the command prompt? The set PATH instruction is only applied to the current command window.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I closed it after the first time I wrote it with "javac -v", Thanks for the information.

Did it again the right way:
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks more like it Now try running your program.

Look in the Java™ Tutorials and the Oracle installation instructions. About halfway down it tells you how to set the PATH. But put your new PATH entry at the beginning of the system PATH. Unfortunately you will have to update it when the next version of Java® becomes available.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my "Shelf.jar"
does nothing when I double click it, But let me read those first I got alot of info to read...
I'll post if nothing goes

Thank you very much so far!
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... mine didn't work either. Maybe there's something I don't know about creating an executing jar in BlueJ. It did work when I did

 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well hey thats the first time I see my program not through IDE *_*
I wrote this:
Knute Snortum wrote:


But yea I really want to make it work by simply double clicking the jar file, Dont get me wrong.. I dont care if its through bluej netbeans .. or w/e...


 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I have it. It goes back to that "GUI" comment someone made. If you type this at a command line:

you will probably get something like this:
jarfile="C:\Program Files\Java\jre1.8.0_60\bin\javaw.exe" -jar "%1" %*

That says basically that you should execute javaw to launch a jar file. But javaw doesn't create a command window! So you can't see a project that only sends output to System.out.println. It works best with GUI projects where you don't want a command window opened.

The solution on your PC is to just execute

but this isn't going to work well if you want to send it to someone else. You could create a batch file like this

and save it as MyMainClass.bat.
 
Arik Shalito
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you thats what I need!!

Thanks everyone! I love this forum :]
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!