Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 30

Thread: FAQ: Compiling and Running Java Programs

  1. #11
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling and Running Java Programs

    These FAQ's are just how to get thing working on Ubuntu, not intros to the languages. The compiling of the sample programs are just to show it works (and a general howto)

    My wiki is dedicated to learning, and those links are on it.

  2. #12
    Join Date
    Aug 2006
    Location
    Pittsburgh, PA
    Beans
    424
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: FAQ: Compiling and Running Java Programs

    We should also add some information abotu setting the CLASSPATH. That can be a major source of issues for newbies and experienced programmers alike.

    There are several ways to do this and I am not sure which one is best really. I know the path is: /usr/lib/jdk1.6.0/bin

    Any suggestions on which way to set the CLASSPATH.

  3. #13
    Join Date
    Oct 2005
    Location
    Oklahoma
    Beans
    427

    Re: FAQ: Compiling and Running Java Programs

    Quote Originally Posted by themusicwave View Post
    Any suggestions on which way to set the CLASSPATH.
    For larger projects you should define the classpath through a project definition via ant, maven, or your IDE.

    For small projects it should be fine to just use the -cp option to javac, perhaps from a shell script for convenience.

  4. #14
    Join Date
    Dec 2006
    Beans
    7

    Re: FAQ: Compiling and Running Java Programs

    javac *.java
    This works

  5. #15

    Re: FAQ: Compiling and Running Java Programs

    Quote Originally Posted by LaRoza View Post
    http://drjava.org/

    It works on Linux.
    Thanks. I got it installed.
    However, when I try to compile, It says "No Compiler Available"
    or something similar.
    Any ideas on where the compiler might be?

  6. #16
    Join Date
    Apr 2008
    Location
    Show me state, USA
    Beans
    162
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: FAQ: Compiling and Running Java Programs

    I've never had an IDE with interactive debugging and have had to rely on print statements to tell me where the program is executing and how variables values are changing.

    For example: System.out.println("Here I am. x=" + x);

    Often conditional using a boolean so I can turn the output on and off with a single statement change:
    Code:
    	boolean debug = true;			
    		...
           if(debug){
              System.out.println("Here I am. x=" + x);
    	 }
    To find out how a method is being called, I add the following statementin the method to print the call stack:
    Code:
          try{throw new Exception("who called");}catch(Exception ex) {ex.printStackTrace();}
    You'll need the -g option on the javac compile to have the statement numbers in the code.

    If the method is called many times you may only want to see it once:
    Code:
       boolean oneTime = true;
         ....
        if(oneTime) {
          oneTime = false;
          try{throw new Exception("who called");}catch(Exception ex) {ex.printStackTrace();}
       }

    Finding NullPointerException in large program that doesn't have enough try{}catch blocks:
    Copy the NullPointerException.java class from the source.zip file for your JDK and modify by adding:
    Code:
        // Insert our own special debugging version
        public String getMessage() {
          try{throw new Exception("Where am I");}catch(Exception ex){ex.printStackTrace();}
          return super.getMessage();
        }
    Compile it and put the class file(s) in its own jar file. Prepend that jar file to the classpath for the java program using the -Xbootclasspath option:
    Code:
    java  -Xbootclasspath/p:D:\JavaDevelopment\MyJavaClasses.jar -classpath .... ...
    When you get a NullPointerException, your code will be called with its printStackTrace() showing who called.

  7. #17
    Join Date
    Jan 2007
    Beans
    34

    Re: FAQ: Compiling and Running Java Programs

    Quote Originally Posted by Vladislav Mkrtychev View Post
    Thanks. I got it installed.
    However, when I try to compile, It says "No Compiler Available"
    or something similar.
    Any ideas on where the compiler might be?
    Same thing for me. It says that it is unable to locate 'tools.jar'.

  8. #18
    Join Date
    Apr 2008
    Location
    Show me state, USA
    Beans
    162
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: FAQ: Compiling and Running Java Programs

    It says that it is unable to locate 'tools.jar'.
    What is the "It" you refer to?

  9. #19
    Join Date
    Oct 2005
    Location
    Oklahoma
    Beans
    427

    Re: FAQ: Compiling and Running Java Programs

    Quote Originally Posted by NormR2 View Post
    What is the "It" you refer to?
    I believe they're talking about DrJava. Here is what the quickstart guide has to say:

    "To compile programs in DrJava, you must make sure you have a Java JDK (Java Development Kit) installed on your machine."

    http://drjava.org/quickstartdocs/gettingready.html

    To turtlepaul and vladislav:

    That page talks about manually downloading and installing the Java SDK from Sun, but hopefully you can just use synaptic to install the sun-java6-jdk package.

    Hopefully just installing the JDK package will get things working, but if not then the tools.jar preference mentioned at http://drjava.org/quickstartdocs/preferences.html may need to be set.

  10. #20
    Join Date
    Jan 2007
    Beans
    34

    Re: FAQ: Compiling and Running Java Programs

    I found the tools.jar file and put the location in in the preferences, and it still doesn't work. It says it's incompatible.

Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •