Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java Class!

  1. Apr 7, 2004 #1

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Welcome to Java Class!

    We're going to learn a bit of Java here, one bit at a time, using real world examples to understand what we're doing. My aim is that all of the Java posted here is executable, so you can try it yourself.

    First, before you can make use of any Java programs, you'll need two things: a compiler and a runtime environment. The compiler essentially just converts your text source files into a stream of bytes that can be executed directly by a physical processor, which doesn't have too many smarts. You could open up a hex editor and type in the actual bytes, but that would be very, very difficult! That's why compilers exist: to convert a human-readable program into a dumb-machine-readable one. The human readable file is called source code, and the machine-readable translation is called object code.

    The second part, the runtime environment, is specific to Java. Unlike other languages, your Java code is not compiled for a specific physical processor, like a PowerPC G5 or a Pentium III. Instead, your Java code is compiled to neutral bytecode. This neutral bytecode resembles the PowerPC G5 and Pentium III instruction sets, but is not run directly. Instead, the bytecode is executed by a program that "pretends" to be a physical processor -- it's called a virtual machine.

    Okay, so where do you get these programs? Here: http://java.sun.com/j2ee/1.4/download.html

    You want the Java J2SE 1.4.2 SDK (Java 2, Standard Edition, version 1.4.2, Software Development Kit). The filename, if you're on Windows, is j2sdk-1_4_2_04-windows-i586-p.exe.

    So, download and install that. When you're done, we'll be ready to compile and run our first program.

    - Warren
     
  2. jcsd
  3. Apr 7, 2004 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Okay, so now you've run Sun's fancy installer, and it's all done. What now?

    Look in this directory on your hard drive: C:\j2sdk1.4.2_04

    The most important stuff for you to know is in the "bin" directory, which means "binary," or "executable programs."

    Notably, in the C:\j2sdk1.4.2_04\bin directory, you'll find two programs that will be most useful to us: java.exe, and javac.exe.

    java.exe is the runtime environment, which runs Java programs.

    javac.exe is the compiler, which compiles Java programs from source code.

    You can also walk around the C:\j2sdk1.4.2_04\demo\applet directory. You can launch the .html files, which include the appropriate links to load the applets in a browser window. You can also look at the source code for these applets, in the .java files.

    - Warren
     
  4. Apr 7, 2004 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Now, how about compiling a program? I'm going to describe the old-school method, which uses the command line. Some people use JBuilder or Forte or other front-end development evironments, and you can also, if you'd like. I think it's useful to know what's going on at the bottom.

    Create a new folder for your new project somewhere. Let's say I make a folder called C:\class.

    Inside this folder, I'm first going to put a convenience file: make.bat, which will compile my code.

    make.bat might look like this:
    Code (Text):

    path=c:\j2sdk1.4.2_04\bin
    javac *.java
     
    and it simply runs the javac program on all files ending with .java in the current directory.

    Now, we need to write a bit of Java code. Use this bit, and put it in a file called Test.java:
    Code (Text):

    public class Test {
        public static void main(String[] argv) {
            System.out.println("Hello world!");
        }
    }
     
    Don't worry about what it does just yet -- we just want to get the compiler fired up to make sure it works.

    Now, open up a command line (Start->Run->"command") and get into your class directory (type "cd c:\class").

    Finally, type "make" and hit enter, and you should see it run. You should now have a Test.class file in the your directory. You've compiled your first Java program!

    - Warren
     
  5. Apr 7, 2004 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Now that you've got a Test.class file, you're all ready to run it.

    First, I'll make another convenience file, run.bat, which simply frees me from having to type the full path of the java.exe program.
    Code (Text):

    path=c:\j2sdk1.4.2_04\bin
    java Test
     
    When you type "run" on the command line, you're just running the java.exe program and telling it to run the program called Test, which is inside the Test.class file.

    If it all goes well, you'll see "Hello world!" printed to your screen.

    Let me know if you're following up to here -- you've now gotten past all the difficult first steps in getting your development environment set up, and you're ready to begin learning the Java language.

    - Warren
     
  6. Apr 7, 2004 #5

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The word "program" is used rather arbitrarily. Usually it does indeed mean "source code," but it can also mean "executable file." An executable file, is, of course, the result of compiling source code. Since there's a one-to-one correspondence between source code and the resulting object code, you can use the term "program" interchangably.
    Intelligence. What I meant by executed directly by a physical processor, which doesn't have too many smarts is simply that physical processors are designed to run only very simple steps, like "add this memory block to this memory block," not complex ones like "figure out what this programmer meant." The compiler's job is to figure out what the programmer means and generate code that can be run directly on the dumb processor.
    Object code and machine code are interchangable. Machine language, on the other hand, may refer to assembly language which is basically a mnemonic form of raw machine code that makes it a bit easier to read. Generally, I'd stick with object code or machine code.
    Java is special, because it has a purpose of being able to run on any computer, with any physical processor. You can compile your Java code on Windows and run it on Linux, for example. The way it accomplishes this is to compile to a platform-independent bytecode, which does not directly run on any physical processor. Bytecode is essentially machine code for a processor that doesn't really exist. Then a program called a virtual machine emulates that idealized Java processor on top of a real processor. Each platform (Windows, Linux, Solaris, etc.) has a Java virtual machine written for it. It effectively translates the bytecodes into real physical instructions the processor can run.
    I'm afraid you'll have to defer to your office administrator.
    To open it with a web browser.
    The two convenience files (run.bat and make.bat) are only shortcuts. You could just type "c:\j2sdk1.4.2_01\bin\javac.exe *.java" everytime you want to compile your code, but you'd get tired of that. Instead, I just put those statements inside a file, called run.bat. Then, you can just type "run."

    - Warren
     
  7. Apr 9, 2004 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Make sure that the "Test.class" file ends up in the same directory as your run.bat file. Test.class is created upon compilation in the same place as Test.java.

    Forgive me for not mentioning the case sensitivity!

    - Warren
     
  8. Apr 9, 2004 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What do you get by simply typing "java Test" on the command line?

    - Warren
     
  9. Apr 9, 2004 #8

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    The line 'java Test' invokes java.exe, the Java virtual machine, and runs your Java program, stored in Test.class. If you got a Test.class file, it means compilation succeeded. If you're getting a "ClassDefNotFoundError," it means java.exe is also running properly. I'm not frankly sure why it's not finding Test.class on your machine. What version of Windows are you running?

    You can also try running the command "java -cp . Test" (note the spaces). The dot by itself means "this directory," and "-cp ." means "classpath here in this directory." The classpath is the list of places where the JVM will look for classes. If you run it with "-cp ." you will force the Java virtual machine to look in that directory for its files.

    I'll also attach a zip file with my files, which are apparently exactly like yours.

    - Warren
     

    Attached Files:

  10. Apr 9, 2004 #9

    turin

    User Avatar
    Homework Helper

    98.




    Isn't this the whole point of:

    c:\cd class

    at the beginning of the procedure, though? Does java.exe only look at a specific directory if I type:

    java Test

    on the command line, or does it look all over the computer?
     
    Last edited: Apr 9, 2004
  11. Apr 9, 2004 #10

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, the trouble you're having executing "java Test" may be a result of your running Windows 98.

    Basically, there's an environment variabled called CLASSPATH. This variable contains a list of locations to search for .class files. It may or may not be set properly on your computer. When you just type "java Test," the java.exe program looks at your CLASSPATH variable, which might point to any number of places on your system. There's a good chance the very primitive Microsoft Java stuff that came with Windows 98 is setting up its own CLASSPATH.

    So, to override it, just use the -cp switch on java.exe. In other words, type "java -cp . Test" and it'll force java.exe to look only in the current directory for class files. That should solve the problem. Lemme know what happens.

    edit: I mean, edit the run.bat file so its second line is "java -cp . Test"

    - Warren
     
  12. Apr 10, 2004 #11
    I'm having trouble getting the applet to show. This is my code:

    import java.applet.Applet;
    import java.awt.*;

    public class AppletTime extends java.lang.Object {


    public AppletTime() {
    }

    public class Picture extends java.applet.Applet{
    public void paint(Graphics g){
    for (int x=0;x<251;x++){
    g.drawLine(0, x, 500, x);
    g.setColor(new Color(255,0,x));
    }


    }
    }

    public static void main(String[] args) {
    }

    }



    I dont know what to do?
    I think it has to show in Netscape Explorer, and I read something about making the code a superclass so it can run in Netscape Explorer but I'm not sure how to do it. Any suggestions?
     
  13. Apr 10, 2004 #12

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    First, your applet needs to be a subclass of Applet, not Object.

    Second, you need to write an html document to include the Applet. You then open the html file with IE or Netscape or whatever.

    Please see the examples in the demo folder. If you're using J2SDK 1.4.2, they'll be in c:\j2sdk1.4.2_04\demos.

    - Warren
     
  14. Apr 14, 2004 #13
    empire:
    you could use the appletviewer utility which comes with the JDK to test your applet without writing an html document. of course if you use html not only you get to learn something new, but you can be sure the applet behaves as you intend.
    Also as I know, a applet does not have a main() function, but instead has init(), start() and paint() ... what you have here appears an attempt to start an applet from command line (???)
     
  15. Apr 23, 2004 #14
    Cool! Now, if someone can help exaplain stacks and queues, I'd be straight.
     
  16. Apr 23, 2004 #15
    stack iz a data structure which follows last-in and first-out rule. that means the most recent data stored in the stack will be the first one to go out...
    the queues follow the first-in and first-out rule instead.
     
  17. Apr 25, 2004 #16
    Using threads, how do I make a string blink?
     
  18. Apr 27, 2004 #17
    I think you might find an example in the JDK folder ... there are a couple of applets there.
    But a quick thought is to make a while loop in the run() function in which you wait for t1 milisecs, draw the string, wait for t2 milisecs, erase the visible area .
     
  19. Apr 27, 2004 #18

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Hmmm sorry to have neglected this thread. I need to consider what sort of code to post next... Any suggestions? Turin?

    - Warren
     
  20. Apr 28, 2004 #19
    maybe some basic file operations.... :)
     
  21. Apr 28, 2004 #20

    turin

    User Avatar
    Homework Helper

    chroot,
    Apparently we are all going to use this thread as a message board, so I will make this my new default and leave all of my posts permanent.




    I just appreciate the fabulous job you did to get me started. I have been lately worried about finding a job and so have also neglected this java pursuit.




    My initial/immediate goal is to be able to annimate ... I suppose that entails using java to display a series of gifs in a web browser one after another at a certain rate. I would like to learn how to do that for starters, so maybe you could organize such a program. I looked at (what I think is) the source code in the demo folder of that package I downloaded, but I could make just about 0 sense out of it.

    I got the "hello world" to display, but all I learned from that is how to make the DOS shell display text, which I could do much more immediately before w/o the java.

    I have absolutely no interest at all in annoying blinking text or anything related to web advertisement. Am I barking up the wrong programming language?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Java Class!
  1. Java's Scanner (Replies: 6)

  2. Java Help (Replies: 7)

  3. Java help (Replies: 15)

  4. Java q (Replies: 3)

  5. Java problem. (Replies: 4)

Loading...