views:

651

answers:

8

I coded up 4 .java files. The thing is that I can only execute my .java files from my IDE, how do I execute the .class files like an application? I study at uni, and I was told that Java is platform independent. Any tutorial/book recommendations would be highly appreciated.

Thanks

+5  A: 

Look at executable jar. Here is the precise one, without any details, Creating executable jar files.

Adeel Ansari
+3  A: 

Java files have to run through the Java Virtual Machine, so you can run your class files from the command line.

If you have a file called filename.java you compile it to filename.class and then you can run it from a command line by typing java filename

Goog
+8  A: 

The basic idea (to give you some things to search for) is:

  • Bundle your compiled .class files into a 'jar'.
  • Add a manifest to your jar specifying a main class to run.

You might find your IDE already creates this when you run a 'clean build'. Netbeans puts this into a 'dist' folder.

Modern JREs will allow you to run the jar by double clicking it etc.

You can also go a bit further by wrapping the jar in a native executable using a tool such as JSmooth.

Cogsy
+1 for pointing out native executable, and Netbeans jar creation.
Adeel Ansari
+4  A: 

What IDE are you using?

Depending on the IDE, some support Export features that will create the .jar executable for you. For example, in Eclipse, you've got that option. Plus there are additional plug-ins for Eclipse, such as Fat-Jar, that will include any additional libs you include that aren't part of Sun's standard libs.

kchau
+1  A: 

JNLP/Web Start

l_39217_l
+2  A: 

If you want to distribute your application on Windows, look into JSmooth.

Alan
+1  A: 

"The basic idea (to give you some things to search for) is:

Bundle your compiled .class files into a 'jar'. Add a manifest to your jar specifying a main class to run. You might find your IDE already creates this when you run a 'clean build'. Netbeans puts this into a 'dist' folder." (by Cogsy)

Plus to achieve this you can either choose:

Depending on the IDE, some support Export features that will create the .jar executable for you. For example, in Eclipse, you've got that option. Plus there are additional plug-ins for Eclipse, such as Fat-Jar, that will include any additional libs you include that aren't part of Sun's standard libs. (by kchau)

Or if you going to to serious stuff, opt for a build script like Ant or Maven. Here's an example of an Ant build.xml script:

<project name="jar with libs" default="compile and build" basedir=".">
    <!-- this is used at compile time -->
    <path id="example-classpath">
     <pathelement location="${root-dir}" />
     <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
    </path>

    <target name="compile and build">
     <!-- deletes previously created jar -->
     <delete file="test.jar" />

     <!-- compile your code and drop .class into "bin" directory -->
     <javac srcdir="${basedir}" destdir="bin" debug="true" deprecation="on">
      <!-- this is telling the compiler where are the dependencies -->
      <classpath refid="example-classpath" />
     </javac>

     <!-- copy the JARs that you need to "bin" directory  -->
     <copy todir="bin">
      <fileset dir="D:/LIC/xalan-j_2_7_1" includes="*.jar" />
     </copy>

     <!-- creates your jar with the contents inside "bin" (now with your .class and .jar dependencies) -->
     <jar destfile="test.jar" basedir="bin" duplicate="preserve">
      <manifest>
       <!-- Who is building this jar? -->
       <attribute name="Built-By" value="${user.name}" />
       <!-- Information about the program itself -->
       <attribute name="Implementation-Vendor" value="ACME inc." />
       <attribute name="Implementation-Title" value="GreatProduct" />
       <attribute name="Implementation-Version" value="1.0.0beta2" />
       <!-- this tells which class should run when executing your jar -->
       <attribute name="Main-class" value="ApplyXPath" />
      </manifest>
     </jar>
    </target>
</project>
Lucas -luky- N.
+1  A: 

Just get out of the IDE and familiarise yourself with the command line tools. The java tutorial has a trail on the subject here (pick the Windows or Solaris/Linux section).

agnul