Class files and Executables

Hey,

Simple question here. I have a java program that I created on a unix machine.

To make a long story short: I created a few .java files. I compiled them into .class files. However, at that point, I expected to see something executable / something I could run.

Is this the wrong line of thinking?

Can I make these .class files executable? If so whats the proceedure for doing so?

Thanks!
Matt

Comments

  • [b][red]This message was edited by Vilanye at 2005-9-30 14:12:49[/red][/b][hr]
    : Hey,
    :
    : Simple question here. I have a java program that I created on a unix machine.
    :
    : To make a long story short: I created a few .java files. I compiled them into .class files. However, at that point, I expected to see something executable / something I could run.
    :
    : Is this the wrong line of thinking?
    :
    : Can I make these .class files executable? If so whats the proceedure for doing so?
    :
    : Thanks!
    : Matt


    You can't really create an executable java file. There are ways to do it, but are hit and miss and defeat the purpose of java. I have never used it, but you can try JIT(Just in Time compiler). Look it up at suns website.

    To execute a basic java file, you can do so on the command line >java someclass, or if you are using an IDE it will execute it for you, these are good for testing and debugging. You can create a executable jar file, go to java.sun.com to find out how. You could also create a .bat file(or whatever the *nix equivelent is called). If you have a servlet, you will need a servlet container(JRun, Tomcat, ect), if you are trying to run an applet, you will need the appletviewer or a java compatible browser.

    The thing to remember is that java programs are not run directly by the processor, like other compiled languages. The java program is run in an interpreter(java virtual machine), and the JVM is run by the processor. So to run a java program, the JVM needs to be running.
    [italic][blue]Just my 2 bits[/blue][italic]



  • : To make a long story short: I created a few .java files. I compiled them into .class files. However, at that point, I expected to see something executable / something I could run.


    .class files run on a virtual machine; that is, you need another application (Java Runtime Environment) to translate instructions to the native platform.

    So, for a file HelloWorld.java:
    [code]
    public class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello, World!");
    }
    }
    [/code]

    compile:
    [code]
    javac HelloWorld.java
    [/code]

    execute:
    [code]
    java HelloWorld
    [/code]

    If you have gcj installed (see gcc), you can compile straight to a native executable. However, you won't be able to run this file on another platform.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • Thanks for the reply!

    I was able to compile and execute the HelloWorld.java program. One problem I had was that I was trying to execute with the command "java HelloWorld.class" instead of "java HelloWorld."

    With the application I was workign on, however, I get the error below. Ever seen something like that before?

    [blue] Exception in thread "main" java.lang.NoClassDefFoundError: Server (wrong name: demo/hello/Server)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    [/blue]


    : : To make a long story short: I created a few .java files. I compiled them into .class files. However, at that point, I expected to see something executable / something I could run.
    :
    :
    : .class files run on a virtual machine; that is, you need another application (Java Runtime Environment) to translate instructions to the native platform.
    :
    : So, for a file HelloWorld.java:
    : [code]
    : public class HelloWorld {
    : public static void main(String[] args) {
    : System.out.println("Hello, World!");
    : }
    : }
    : [/code]
    :
    : compile:
    : [code]
    : javac HelloWorld.java
    : [/code]
    :
    : execute:
    : [code]
    : java HelloWorld
    : [/code]
    :
    : If you have gcj installed (see gcc), you can compile straight to a native executable. However, you won't be able to run this file on another platform.
    :
    : ---------------------------------
    : [size=1](Its just my sig)
    : HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :
  • : [blue] Exception in thread "main" java.lang.NoClassDefFoundError: Server (wrong name: demo/hello/Server)
    [/blue]

    Sounds like a classpath/package issue (is Server defined in a package called demo.hello?).

    OK, say you are developing some files in $HOME/jproj

    [code]
    package demo.hello;
    public class HelloWorld {
    public static void main(String[] args) {
    System.out.println("Hello, World!");
    }
    }
    [/code]

    This code must be in a file called $HOME/jproj/demo/hello/HelloWorld.java

    To compile, use the commands:
    [code]
    cd $HOME/jproj
    javac demo/hello/HelloWorld.java
    [/code]

    This creates a file $HOME/jproj/demo/hello/HelloWorld.java

    To execute, use the commands:
    [code]
    java -classpath $HOME/jproj demo.hello.HelloWorld
    [/code]

    To include other libraries you depend on:
    [code]
    cd $HOME/jproj
    javac -classpath /opt/somelib.jar:/opt/anotherdirectory:/opt/somelib2.zip demo/hello/HelloWorld.java
    [/code]
    [code]
    java -classpath $HOME/jproj:/opt/somelib.jar:/opt/anotherdirectory:/opt/somelib2.zip demo.hello.HelloWorld
    [/code]

    Basically, in a directory the base of the classpath must correspond to the base of the package. If you archive your class files into a ZIP or JAR, you must preserve this directory structure.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

  • Wow.

    That was a very clear way to describe it. I've looked all over the web and haven't found an explanation that clear and concise.

    Thank you greatly!



    : : [blue] Exception in thread "main" java.lang.NoClassDefFoundError: Server (wrong name: demo/hello/Server)
    : [/blue]
    :
    : Sounds like a classpath/package issue (is Server defined in a package called demo.hello?).
    :
    : OK, say you are developing some files in $HOME/jproj
    :
    : [code]
    : package demo.hello;
    : public class HelloWorld {
    : public static void main(String[] args) {
    : System.out.println("Hello, World!");
    : }
    : }
    : [/code]
    :
    : This code must be in a file called $HOME/jproj/demo/hello/HelloWorld.java
    :
    : To compile, use the commands:
    : [code]
    : cd $HOME/jproj
    : javac demo/hello/HelloWorld.java
    : [/code]
    :
    : This creates a file $HOME/jproj/demo/hello/HelloWorld.java
    :
    : To execute, use the commands:
    : [code]
    : java -classpath $HOME/jproj demo.hello.HelloWorld
    : [/code]
    :
    : To include other libraries you depend on:
    : [code]
    : cd $HOME/jproj
    : javac -classpath /opt/somelib.jar:/opt/anotherdirectory:/opt/somelib2.zip demo/hello/HelloWorld.java
    : [/code]
    : [code]
    : java -classpath $HOME/jproj:/opt/somelib.jar:/opt/anotherdirectory:/opt/somelib2.zip demo.hello.HelloWorld
    : [/code]
    :
    : Basically, in a directory the base of the classpath must correspond to the base of the package. If you archive your class files into a ZIP or JAR, you must preserve this directory structure.
    :
    : ---------------------------------
    : [size=1](Its just my sig)
    : HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories