View Full Version : Running Java programs 'externally'
derby007
January 29th, 2007, 11:11 AM
Q1. How do I run the code/program 'outside' of the IDE, eg. from the command line ?
I tried using: javac test.java
but got an error: Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tools/javac/Main
: is it looking for something in this folder, which i cant find on the system?
Note: i used Sun Enterprise Edt 8, and i have the javac, java programs...
Q2. What can i do with '.jar' files?
Q3. Maybe someone might EXPLAIN the whole Java thing, can i use different graphics for my 'runnable' or 'executable' file???
/*
* test.java
*/
package my.test;
import javax.awt.*;
import javax.swing.*;
public class test extends javax.swing.JFrame {
/** Creates new form test */
public test() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 70, 90));
jButton1.setText("enter");
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 40, -1, -1));
pack();
}
// </editor-fold>//GEN-END:initComponents
/** * @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}
lnostdal
January 29th, 2007, 11:20 AM
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/unix.html
I do not understand Q2 ( http://en.wikipedia.org/wiki/JAR_%28file_format%29 ?) and Q3.
fabian.vogler
January 29th, 2007, 11:25 AM
A1: Try to avoid usage of depended classes like "org.netbeans.lib.awtextra.AbsoluteLayout" and use swing classes (e.g. java.awt.BorderLayout):
package my.test;
public class test extends javax.swing.JFrame {
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
public test() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
getContentPane().setLayout(new java.awt.BorderLayout());
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
jLabel1.setText("jLabel1");
getContentPane().add(jLabel1, java.awt.BorderLayout.NORTH);
jButton1.setText("enter");
getContentPane().add(jButton1, java.awt.BorderLayout.SOUTH);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test().setVisible(true);
}
});
}
}
A2: By executing "javac test.java" and new file "java.class" gets compiled. With a JAR-File you can bundle multiple *.class-Files like in a ZIP-File. (For more information see post above.)
A3: Again, see post above... ;-)
derby007
January 29th, 2007, 12:05 PM
Cheers fabian your tip worked.......after i also updated my PATH to the correct /bin directory.
Q. If I'm using SunDevStudio or Netbeans & if it writes code like :
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
Can I set it up to avoid this happening?
ie. i'm a bit clueless with libraries etc. as it can get a bit confusing following tutorials/examples.
Basically i starting a GUI frontend to talk to a Backend Database !!
Any suggestions?
fabian.vogler
January 29th, 2007, 01:14 PM
Glad to hear it worked ... and you even managed to fix your Java compiler. ;-)
I actually prefer developing with Eclipse and the Eclipse Visual Editor (http://www.eclipse.org/vep/) so I don't know much about Netbeans - but there's might an option to change this behavior in the preferences.
In general I think it's a good idea to start with Netbeans, because it supports you a lot in creating GUI applications and it doesn't require much knowledge of the way Java works and you can create in a short period of time nice applications.
The problem is (as I pointed to) you don't learn what Java is all about. You're able to say "Well I'd like this library and this ... go ahead and add them - I want to use them!". If you're on the command line and you need to compile and launch your application on your own then you need to know how to set the classpath (http://java.sun.com/docs/books/tutorial/essential/environment/paths.html and http://java.sun.com/j2se/1.3/docs/tooldocs/win32/classpath.html) and how to launch JAR-Files...
At all I suggest you to keep using Netbeans to make some progress - but also to try to find the libraries (most of the time JAR-Files) and compile and launch the application on your own.
derby007
January 29th, 2007, 02:05 PM
Again cheers, at last some meaningful help,
i had been browsing my little eyes out to try & get the right info,
theres TOOOOO much help out there at times, & its overwhelming.
Maybe it was just 1 of those days.....
Last Q: should i go down the Java Application
OR the Java Applet way of doing the frontend ?
any advantages or dis-adv with either.. ? !! ? :D
lnostdal
January 29th, 2007, 03:38 PM
Last Q: should i go down the Java Application
OR the Java Applet way of doing the frontend ?
any advantages or dis-adv with either.. ? !! ? :D
I'm voting for Java Application or Java Web Start. Applets that crash my browser drives me nuts; yes it still happens. :)
fabian.vogler
January 29th, 2007, 04:24 PM
I'm voting for Java Application or Java Web Start. Applets that crash my browser drives me nuts; yes it still happens. :)
Same here! :D
IMHO applets are stupid ... they can be useful sometimes - but in times of Web 2.0 and AJAX applets are slow and ugly. With applets you're also very limited because you can't use cool things like SWT (http://eclipse.org/swt) and the user needs to allow you to access his file system.
Another interesting (but pretty complicated and confusing) thing you can do with java is building web applications: Java EE (Enterprise Edition) (http://java.sun.com/javaee/). But keep this topic in mind for later ... it experienced on my own how long it can take to understand Java EE. ;-)
phossal
January 29th, 2007, 05:56 PM
Q2. What can i do with '.jar' files?
.jar files are cool. You can combine your entire program in a single jar. You can deploy it as a web app (as you might using Tomcat). In certain environments, a single jar can be both executable, and shared among many machines on a network drive, as each machine will run it's own "version" in the its respective JVM.
It can contain graphics (pictures, window icons, etc), and sounds, and basically everything you might want to package with it. ;)
derby007
January 30th, 2007, 03:51 AM
".jar files are cool" : can u explain this for me, this is where i loose momentum!
If i start up Netbeans, get a GUI going, compile it, & run it within the IDE.....this then creates a java file, class file, jar file, libraries etc. BUT i then want to run this outside of the IDE or on another PC, WHAT DO I DO ????? pls help.....:)
phossal
January 30th, 2007, 04:01 AM
".jar files are cool" : can u explain this for me, this is where i loose momentum!
If i start up Netbeans, get a GUI going, compile it, & run it within the IDE.....this then creates a java file, class file, jar file, libraries etc. BUT i then want to run this outside of the IDE or on another PC, WHAT DO I DO ????? pls help.....:)
Well, it's easy to lose a little momentum when tackling such a topic. There aren't very many clear answers because you're not ready to ask very specific questions yet.
The simplest use of jar file is just a package. They're like zip files. There is no secret, no trick to that.
The next use for a jar file is for launching the application (the classes) inside of it. This can be done using this command:
java -jar name_of_jar.jar
In order for it to work, the .jar file must also include a manifest attribute file. Normally, a good IDE will prepare this for you. So you just export a jar holding your application from the IDE, and it's runnable.
The next use of a .jar is in a web application. You might use a .jar file to make a web application available using Tomcat. That's more complicated, so I won't describe it here.
And it gets progressively more complicated...
derby007
January 30th, 2007, 04:47 AM
Can u show me an example of a manifest.mf file with entries in it.....
ie. i got this from a 'how-to' online:
"Exception in thread "main" java.lang.NoClassDefFoundError: org/jdesktop/layout/GroupLayout$Group
Ensure that the manifest.mf file references the currently installed version of the Swing Layout Extensions Library. "
phossal
January 30th, 2007, 04:53 AM
This is the MANIFEST.MF file from eclipse, which is typically found in /usr/lib/eclipse/startup.jar
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-14ubuntu7) (Free
Software Foundation, Inc.)
Main-Class: org.eclipse.core.launcher.Main
There are a lot of tutorials online about working with Jar files. Again, your IDE should do this for you.
derby007
January 30th, 2007, 04:59 AM
ah, your a great help.........i thought the IDE would aswell, but any manifest file i opened had no mention of 'MainClass'
vBulletin® v3.8.7, Copyright ©2000-2012, vBulletin Solutions, Inc.