Jump to content
xisto Community
Sign in to follow this  
IWroteCode

Java Prog Won't Run

Recommended Posts

Hey guys, I just have a basic question about Java - the teacher I have at school just isn't getting her point across.

I have the JDK and JRE 5.xx version installed into my Program Files folder.

I have a folder in My Documents named "Java Programs" where I store the .java and .class files.

Supposedly I need a batch file called JDKsetup.bat that my school provided. I don't know where to place this file in my computer or what it's used for.

I can compile my program but cannot run it - I know I'm supposed to set some environment variables and paths but am not sure how to go about it.

My code is as follows:

/* 	Program DisplayMessage	The program displays messages*/import javax.swing.*;class DisplayMessage{	public static void main(String[] args)	{  String name;  JFrame myWindow;  //declare a name  myWindow = new JFrame();	//create an object  myWindow.setSize(400,300);	//send a message to it  myWindow.setTitle("My first Java Program");  myWindow.setVisible(true);    //output messages  JOptionPane.showMessageDialog(myWindow, "I love Java");  JOptionPane.showMessageDialog(null, "Goodbye");    //input name and display a message with the name  name = JOptionPane.showInputDialog(null, "Enter your name: ");  JOptionPane.showMessageDialog(null, "Hello " +name+ ".");	}}

Thanks for the help.

Share this post


Link to post
Share on other sites

runs just perfect on my system. all i did was javac DisplayMessage.javaand then java DisplayMessagemake sure u do this where u have the DisplayMessage.class. Also you dont have any event catcher ..ie after the close window the program keeps on running. So eith put a System.exit(0); as last statement ..or better catch the window close event and then call System.exit(0);what are your outputs of javac and java? plain . What OS are u running on?

Share this post


Link to post
Share on other sites

So you didn't need the JDKsetup.bat? I wonder what it was for then...

 

 

My outputs of java and javac? What do you mean?

-both of the .exes are in the folder in programs files.

 

I'm running Windows XP Home.

 

 

 

PS: What code would I put in to catch the window close event and exit?

Share this post


Link to post
Share on other sites

So you didn't need the JDKsetup.bat?  I wonder what it was for then...

My outputs of java and javac? What do you mean?

-both of the .exes are in the folder in programs files.

 

I'm running Windows XP Home.

PS: What code would I put in to catch the window close event and exit?

1064323577[/snapback]


Can you please post the content of JDKsetup.bat, so we can understand what it is trying to do?

 

As the previous thread said, to compile you need to use "javac filname.java" and to run only "javac filename", if the compilation was succesfull..

 

Thanks

Share this post


Link to post
Share on other sites

You might also want to tell how you compiled the program and tried to run it (i.e. what commands you used, and whether you used the command line or and IDE).

 

In the regards of environment variables, you'll need it if you use just javac or java, without full directory information (such as C:\Program Files\Java\bin\javac). Perhaps the .bat file is trying to set them so that you don't have to do it yourself?

 

As for the classpaths, that is only necessary if you have multiple classes. Then, you'll need to call set classpath=. so that the java interpreter can find the other classes.

 

Hope this helps.

Share this post


Link to post
Share on other sites

Most likely you don't have the environment variables well set up. You'll need to define at least the PATH variable. It can be useful to define the CLASSPATH variable as well, but not really necessary as long as you compile and run your program with the -cp flag (like this: javac -cp <place where your .class files are> program.java)If you want to define the PATH environment variable in Windows XP just do this:1. Right-click on My Computer and click Properties.2. Click the Advanced tab.3. Click the Environment Variables button below.4. If PATH isn't already defined, click the New button on the User Variables block (or System Variables if you want every user to have it).4.1). On Variable Name type PATH (all caps) and on Variable Value type the path to the bin folder of the JDK. Something like: "C:\Program Files\Sun\JDK-1.5.0\bin;"4.2) If PATH is already defined, select it and press Edit. Next add the path to the bin folder right after the ";". DON'T GIVE ANY SPACES BETWEEN ; AND THE PATH OR IT WON'T WORK!5. Press OK all the way back to the Desktop. You're done! :o

Edited by KazDoran (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.