View Full Version : Running a Java program on another PC - simply
jakenoble
06-08-2005, 11:36 PM
Hi all.
CrossPosts:
Here (http://forums.devshed.com/showthread.php?t=262207&page=1)
and
Here (http://forum.java.sun.com/thread.jspa?threadID=634527&tstart=20)
If I write a Java program on my PC, write it, compile it, then Java it from the command prompt, how do I get this program to then run simply on someone elses computer? The other person would be using Win XP.
E.g. not having to install the SDK, then using Run, then using the Java command?
I want to do this because an ex-teacher of mine has asked me to write some programs for him, but also to give me some more experiance. Anyway he won't know how to use Java at all, so I was looking for the simplest way of giving him a file and saying "Here it is, do x,y,z and your away"
Short of getting into GCJ is Jar the best way to go in my situation?
Thanks
Jake
suryad
06-09-2005, 01:38 AM
If I understand your problem correctly, and I hope I do, there is as far as I know, no way of running a Java program unless there is a runtime/JVM installed in the target machine. That is the whole point of Java with the write once and run anywhere policy. They forget to mention that premise will only hold if the machine in question has the required runtime installed on it, so in this case your professor will have to go to java.sun.com and to the downloads section and get himself a copy of the runtime at least if not the JDK because the JDK would be required for development and not only for running programs.
jakenoble
06-09-2005, 09:14 AM
This is what I thought, I downloaded the JRE onto someones else computer and ran java -jar Filename.jar from the cmd line and it seemed to work ok.
He should be able to manage that, is there anyway of making it an exe?
suryad
06-09-2005, 10:50 AM
There are ways of making a java file after compiling to become a native .exe file but I think they cost a lot of moolah :rolleyes: and I am not aware as of yet of any existing open source java bytecode to native .exe translators.
suryad
06-09-2005, 10:51 AM
Er...dont mind me asking but I am curious as to what kind of java programs you are writing. I am a relative noob meself and am trying to soak up as much bit of java information as is possible... :p
jakenoble
06-09-2005, 03:23 PM
He baasically wants a sort of all in one package that teachers and students can use. It'll include several things:
A database of economics and business studies key terms, which can be added, removed, edited and the students can be tested on, then results accessed by the teacher.
A large clock display to perfect student exam timings during exam mocks.
A random picture generator, which displays pictures of students, when the a student is displayed they are the one that has to answer the question, basically to make it more fun and involving.
Jake
suryad
06-09-2005, 11:41 PM
That sounds quite innovative, I take it it will be a GUI and thus you are probably using Swing or SWT or something like that?
jakenoble
06-10-2005, 01:27 AM
I thought so too, not an amazing program, but useful! Swing it'll be.
suryad
06-10-2005, 09:04 AM
Hey I found this article...if you are on the Windows platform then this could help a lot. Very nice stuff: http://www.javalobby.org/forums/thread.jspa?threadID=15869&tstart=0
Spookster
06-10-2005, 03:14 PM
If by making it an exe you want to be able to just double click on the program to run it you can already do this using the jar file. You can make the jar file executable by setting up the manifest file to run the main class. It's been years since I've done it but that is what we did in college to make them act like exe files.
suryad
06-11-2005, 01:06 AM
Agreed, Spookster. But he was trying to make it easier on the person so that they wont need to install jdk or a jre at all...so basically a .exe and it would...or should just work. I have not tried this out yet but yeah I know what you mean as to dbl clicking the jar file and having the app run provided the environment is set up right including the classpath and the path and so on.
jakenoble
09-23-2005, 03:42 AM
Back to this again. I have been away from my PC for a few months and still cannot do this.
I have three files
class driver
{
public static void main (String [] args)
{
MainPanel m = new MainPanel();
}
}
AND
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MainPanel extends JFrame{
Container container = getContentPane();
GetInput g = new GetInput();
public MainPanel()
{
super();
setTitle("Random Students!");
setDefaultLookAndFeelDecorated(true);
setSize(800,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.setLayout(new BorderLayout());
container.add("Center",g);
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
setVisible (true);
}
}
AND
import javax.swing.*;
import java.awt.*;
import java.lang.Object.*;
import java.io.*;
import java.awt.event.*;
class GetInput extends JPanel implements ActionListener{
String arayFileNames;
int randomNum;
int numberOfFiles;
JFileChooser chooser;
String choosertitle;
String chosenDir;
String dir;
int lastImage=-1;
JButton displayButton = new JButton();
JLabel label1 = new JLabel("Please enter the directory for the pictures you wish to use: ");
JButton browseButton = new JButton("Browse");
JButton button1 = new JButton("Submit");
JTextField textField1 = new JTextField(40);
JLabel label2 = new JLabel("");
JLabel blank = new JLabel("");
List fileNames = new List();
public GetInput(){
button1.addActionListener(this);
displayButton.addActionListener(this);
browseButton.addActionListener(this);
add(label1);
add(textField1);
add(button1);
add(blank);
add(browseButton);
add(label2);
setVisible (true);
}
public void actionPerformed (ActionEvent e){
if(e.getSource() == button1){
if(textField1.getText() .equals("") ){
label2.setText("Please enter a valid directory");
}
else{
dir = textField1.getText();
File myDir = new File(dir);
String[] arrayFileNames = myDir.list();
for(int i=0; i < arrayFileNames.length; i++){
arrayFileNames[i] = dir + "\\" + arrayFileNames[i];
System.out.println(arrayFileNames[i].substring(arrayFileNames[i].length()-3));
if(arrayFileNames[i].substring(arrayFileNames[i].length()-3) .equalsIgnoreCase("jpg")){
fileNames.add(arrayFileNames[i]);
}
else if(arrayFileNames[i].substring(arrayFileNames[i].length()-4) .equalsIgnoreCase("jpeg")){
fileNames.add(arrayFileNames[i]);
}
else if(arrayFileNames[i].substring(arrayFileNames[i].length()-3) .equalsIgnoreCase("gif")){
fileNames.add(arrayFileNames[i]);
}
else{}
}
numberOfFiles = fileNames.getItemCount();
randomNum =(int)(Math.random() * (double)(numberOfFiles- 1)) + 1;
ImageIcon image = new ImageIcon(fileNames.getItem(randomNum));
displayButton.setIcon(image);
textField1.setVisible(false);
button1.setVisible(false);
label1.setVisible(false);
label2.setVisible(false);
blank.setVisible(false);
browseButton.setVisible(false);
add(displayButton);
displayButton.setVisible(true);
}
}
else if (e.getSource() == displayButton){
randomNum =(int)(Math.random() * (double)(numberOfFiles- 1)) + 1;
lastImage = randomNum;
ImageIcon image = new ImageIcon(fileNames.getItem(randomNum));
displayButton.setIcon(image);
}
else if (e.getSource() == browseButton){
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("C:\\"));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
chosenDir = chooser.getSelectedFile().toString();
textField1.setText(chosenDir);
}
else {
}
}
else{
}
}
}
/*
javac *.java
jar cmf MANIFEST.MF rim.jar driver.class MainPanel.class GetInput.class
java -jar rim.jar
*/
My manifest files looks like this
Manifest-Version: 1.2
Main-Class: driver
Created-By: 1.4 (Sun Microsystems Inc.)
From the Cmd line I have been running this
jar cfv randomImages.jar *.java and
jar cfv randomImages.jar *.class
Both times I get, "Main class cannot be found. Program will Exit." I have looked for solutions in many places and found them. But I cannot get it to work what ever I do. I have tried pressing enter, etc at the end of the Manifest file as I know its very picky about that.
Thanks for ANY input
Jake
Roelf
09-23-2005, 08:55 AM
My manifest files looks like this
Manifest-Version: 1.2
Main-Class: driver
Created-By: 1.4 (Sun Microsystems Inc.)
Is this the full path to your mainclass? Or do you have it hidden in a package in the jar?
What you specify here should be the full name to the class, including any subfolder you created in the jar.
Where did you put the manifest file?
jakenoble
09-23-2005, 12:07 PM
I Have now changed the manifest file to look like
Manifest-Version: 1.2
Main-Class: C:\Documents and Settings\Jacob\My Documents\Work\Java Programming\Random Images\driver
Created-By: 1.4 (Sun Microsystems Inc.)
And this still does not work, the driver file does not exist in a jar file, just in a directory on my C drive. does the driver file location need to end in .java or .class?
Thanks for your time Roelf.
Jake
Roelf
09-23-2005, 12:33 PM
When you execute the jar command, in the second way you tried:
jar cfv randomImages.jar *.class
the result of this should be a jar file with your classes in it. No manifest though, check the documentation (type jar on a command line, you should look at example 2) to see what to do to create a correct manifest in the jar-file. The way you are jarring these classes, your original Main-class identification was correct:
Main-Class: driver
after creating the jar, you can look in it using winzip or such a tool. Check if the .class files are in there, check also if there is a Meta-inf folder in it, containing your manifest.mf
jakenoble
09-23-2005, 08:34 PM
Hi again,
Right done that, opened the created Jar file with WinRAR, it contains the three class files as well et the META-INF Directory. But the MANIFEST file inside this dir readsManifest-Version: 1.0
Created-By: 1.4.2_08 (Sun Microsystems Inc.)
Without the "Main-class: driver" line?
Edit: I just noticed the versions are different, is it creating a completely new file and not using my MANIFEST at all?
Is this correct?
Jake
Roelf
09-23-2005, 09:01 PM
It reads your manifest file and puts the info in the file made by jar.exe, strange though that the Main-Class is not put in. Is the spelling correct? Correct upper/lower case in your manifest file?
Edit the version created by jar.exe, add the line yourself, does it work then?
Line should be like:
Main-Class: driver
jakenoble
09-24-2005, 12:26 PM
Roelf, do you mean editing the .MF file myself then putting it back in the jar file using WinRAR. If you do mean that I have tried that and gotten no where.
Thanks again for your input and time.
jake
Spookster
09-25-2005, 05:06 AM
I don't see where you are including your manifest file inside your jar file.
Let's try this. In your manifest file put this:
Manifest-Version: 1.0
Main-Class: driver
Make sure you put a carriage return in at the end of your manifest file. So just hit the enter key at the end of the main class line.
BTW in java it's standard to capitalize the first letter of your classes when you name the file.
Now put all of your .java and .class files and your manifest file in the same directory and in your command prompt go to that directory and type this:
jar cvfm driver.jar mymanifestfile.txt *.java *.class
Obviously change mymainfestfile.txt to whatever you named your manifest file.
jakenoble
09-25-2005, 01:26 PM
Done that Spookster. But still no luck, same error.
My Manifest file now reads
Manifest-Version: 1.2
Main-Class: Driver
Created-By: 1.4 (Sun Microsystems Inc.)
If I then get this out of the jar file with WinRAR it reads correctly, as it should. My cmd line reads
jar cvfm driver.jar MANIFEST.MF *.java *.class
I have attached my java files as txt files, as you cannot attach java files. I have also attached my Manifest.mf file, as a txt file also.
Jake
Spookster
09-25-2005, 06:39 PM
You are doing everything correctly at this point as far as I can see. The error is saying it cannot find the main class? What was the exact error message?
jakenoble
09-25-2005, 07:44 PM
Could not find the main class. Program will Exit.
is the exact error message.
Spookster
09-26-2005, 01:00 AM
Are you getting that message when you double click the jar file or are you also getting it when you run the jar from the command line
java -jar driver.jar
If you are getting the error only from double clicking try adding the class path attribute to your manifest file.
Manifest-Version: 1.2
Main-Class: Driver
Created-By: 1.4 (Sun Microsystems Inc.)
Class-Path: C:\driver.jar (or wherever you have it at)
You might have a class path issue.
jakenoble
09-26-2005, 01:09 AM
Right I have changed the manifest to read : Manifest-Version: 1.2
Main-Class: Driver
Created-By: 1.4 (Sun Microsystems Inc.)
Class-Path: C:\Documents and Settings\Jacob\My Documents\Work\Java Programming\Random Images
Error is the same when I double click it. If I run it from the cmd line I get the following error "Exception in thread "main" java.lang.NoClassDefFoundError: Driver"
Thanks again for your time Spookster
Spookster
09-26-2005, 03:46 AM
Ok try taking that class path out of your manifest file. This time set the class path on your system to the location of your jar file. I would also recommend just creating a directory off the root of your C drive and put all your files in there and compile and run them from there so you don't have all the spaces in your directory names. So something like C:\myproject. So then you would set your system classpath to C:\myproject\driver.jar
Once you got that set then try to run it from the command line again. I think you are just having a classpath issue.
brad211987
09-26-2005, 05:49 PM
There may be another solution. If you download the JGrasp program to edit/create your programs and load it up as a project in JGrasp, it will be able to create an executable .jar file for you, I've been doing it with my programs for a little while now and it works well.
Spookster
09-26-2005, 06:06 PM
There may be another solution. If you download the JGrasp program to edit/create your programs and load it up as a project in JGrasp, it will be able to create an executable .jar file for you, I've been doing it with my programs for a little while now and it works well.
If he is having a class path issue then using a GUI to create the executable jar verses creating it from the command line like we are now is going to result in the same error.
brad211987
09-26-2005, 06:35 PM
Still only learning it all myself, figured that maybe there was a small mistake in there somewhere that the GUI would do correctly, I've never done it manually as he's been trying to do myself so I can't offer much help.
Spookster
09-26-2005, 07:13 PM
It's possible that the GUI installation may set up a correct classpath for the files it compiles and runs.
suryad
10-11-2005, 09:39 AM
Have you tried looking at the Runtime class?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.