How to run Java programs? (everyone ought to know this)

I'm very new to Java, and is having some stupid problems.

I've just downloaded JDK1.2.2 and J3D1.2.1. There are quite a couple of demos that came with the downloads. I'm using Win98 by the way.

So how do I try out the demos?
Double clicking on the .java or the .class files doesn't work. Sending them to the Java.exe in JRE also doesn't work.

So how do I run the demos?

Thanks a lot!

>>Ethan<<
ShakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sgomsCommented:
If they are applets(they'll be under a directory called applets) then double click on the HTML file. The browser will run ur applet.

Else,

under the prompt
1.complile java file
>javac <java filename>.java
2.Run java file
>java <java file name without any extensions>

To acess java & javac from any directory u need t ur path & class path in the autoexec.bat

include,
SET PATH=<JAVA HOME>\BIN;
SET CLASSPATH=<JAVA HOME>\LIB;

<JAVA HOME> will most propably be c:\jdk1.2

any further help needed?

-sgoms
0
ShakAuthor Commented:
Well, thanks a lot, but I still can't get it.

Applets are ok. Demos in the c:\jdk1.2.2\demo\jfc directories are also ok. I've found out you just need to double click on the .jar files to run them.

However, I'm trying to try out Java3D demos. The demos are still in .java and .class files. None are in .jar.

I've tried your method, the path works ok. But when I tried to compile, eg
  javac SplineAnim.java
It gives the error:
   error: Can't write: SplineAnim.class


I believe this looks stupid, but I really don't know how this works.
Thanks.
0
Jan LouwerensSoftware EngineerCommented:
the .java file is the actual source code.
it compiles into a .class file, that is interpreted and run by the java interpreter
you prabably copied your files from a cd, and the .class files are set to read-only. make sure the .class fileas are not read only if you wish to recompile them.
since the .class files are already there, the .java files have already been compiled. To run them:

java <class filename WITHOUT extension>

if you do set a classpath, nmake sure you include the rt.jar file (that comes with the jdk) in your classpath
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ShakAuthor Commented:
thanks again, so much for the Read-Only.

But I'm still not able to run the .class file.
It says:
  Exception in thread "main" java.lang.NoClassDefFoundError: SplineAnim


Perhaps this demo isn't a 'full' demo that can be ran by itself....

sorry, but what is this rt.jar file again? And how do i include it in my classpath?

Thanks a lot. Really!

>>Ethan<<

0
sgomsCommented:
rt.jar is the file that contains all the java libraries in jdk1.2 previously it used to be classes.zip for jdk1.1
it'll be present in <java_home>\lib directory.

Just make sure that you have set ur classpath to
set CLASSPATH=<java home>\lib;

where ur java_home is c:\jdk1.2\ or whatever dir uve installed jdk into.

ur error sounds like its caused 'cos u gave,
>java SplineAnim.class

DO NOT INCLUDE .CLASS WHEN you are intepreting.

just give,

>java SplineAnim

still if its not working then maybe this program is not teh startup. try other files in the saem directory.

all the best.
-sgoms
0
ShakAuthor Commented:
I feel so dumb.

Anyway, I've even tried some stupid HelloWorld program, still the same problem.

The compilation is ok, HelloWorld.class is generated, but it can't be run.
It gives this error:
  C:\t>java HelloWorld
  Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld

What can be the mistake?

Here is my autoexec.bat:
  SET PATH=C:\WINDOWS;C:\PROGRA~1\MTS;C:\JDK1.2.2\BIN

SET CLASSPATH=C:\JDK1.2.2\LIB;C:\JDK1.2.2\JRE\LIB\RT.JAR;C:\JDK1.2.2\VISAD\VISAD.JAR


Note: VisAD is a visualization program I'm trying to try out. It says visad.jar is to be included in Classpath as well. Or am I doing it wrong?

Thanks a lot in advance.

0
amitdCommented:
hello shak
why don't u check your class path i think u may not have been set it correctly
best luck
  Amit
0
sgomsCommented:
classpath is fine.
try this,

>c:\autoexec  //run ur autoexec
>set //check if everything is set properly

if after typing set u can see
classpath=c:\jdk1.2.2\lib
then its fine.

BTW, c:\jdk1.2.2 is the dir right?

if u dont see classpath=c:\jdk1.2.2\lib

then just type,
>set classpath=c:\jdk1.2.2\lib & then
>java HelloWorld

still if it does not work then try the program below & tell me if it works.

public class HelloWorld{
  public static void main(String args[]){
    System.out.println("Hello World");
  }
}

save it as HelloWorld.java
compile it javac HelloWorld.java
run it java HelloWorld

what happens?
-sgoms
0
ShakAuthor Commented:
Thanks a lot for your patience.
Here's the observation:

1) Classpath is alright. Typing 'SET' does show the line. (is the syntax right? it is in my previous posting)

2)Your HelloWorld is the same one I tried just now. It can compile, producing HelloWorld.class. But I can't run it with >java HelloWorld
It says :
  Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld



Hope I'm giving enough information here. Please tell me I can help you to help me. Thanks again.
0
sgomsCommented:
try one more thing just place HelloWorld.class inside c:\jdk1.2.2\lib directory & say,
c:\jdk1.2.2\lib>java HelloWorld

just make sure that u dont say java HelloWorld.class

what's happening?
0
ShakAuthor Commented:
You know... I feel really dumb

somehow, the thing is working now. Perhaps I really did something stupid just now.

Anyway, Java3D demos still can't be run like this, don't know why. But at least HelloWorld runs, and my VizAD runs. That's the important thing.

Well, I still don't know what I did wrongly, and thanks a zillion. You've been of tremendous help.

One last question though. Is there any way to use the Windows GUI to speed things up? typing everything in dos prompt is a bit slow I would say.

Anyway,thanks again, and please rememeber to 'answer' this question so i can award the points.

Seeya!
>>Ethan<<

0
ShakAuthor Commented:
You know... I feel really dumb

somehow, the thing is working now. Perhaps I really did something stupid just now.

Anyway, Java3D demos still can't be run like this, don't know why. But at least HelloWorld runs, and my VizAD runs. That's the important thing.

Well, I still don't know what I did wrongly, and thanks a zillion. You've been of tremendous help.

One last question though. Is there any way to use the Windows GUI to speed things up? typing everything in dos prompt is a bit slow I would say.

Anyway,thanks again, and please rememeber to 'answer' this question so i can award the points.

Seeya!
>>Ethan<<

0
ShakAuthor Commented:
p/s would could be the reason for some .class programs which can't be run and gives the error message that I got? Eg. the demos in Java3D, I'm not sure if they're not meant to be run this way, or I'm just doing things wrongly
0
sgomsCommented:
1. Windows GUI: Yes you can use an Integrated Development Environment(IDE) for compiling & running ur java programs.
Microft VJ++, Borland JBuilder, IBM Visual Age..r some of the most widelt used ones.

SitepadPro, JPadpro r some of the IDE's that r simpler.

chk out for free Java IDE's at www.download.com

2. Java3D: maybe u tried intepreting classes that do not have main in them. iam not familar with Java3D classes but it cud be that they just provide a library that u can use in YOUR application.

-sgoms
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.