Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Compiling and Running Java Programs in Terminal

  1. #1
    Join Date
    Jan 2012
    Beans
    25

    Compiling and Running Java Programs in Terminal

    I was learning how to program before I switched to Ubuntu, and as I was using Java for a class I learned how to do all things Java-related in Dr. Java. I probably could install the windows version of this program and run it with WINE, but I was hoping I could perhaps figure out how to compile/run Java programs using the terminal. However, all of my attempts in doing so have been unsuccessful. Is there a chance anyone could explain what commands and/or packages are required to compile and run programs using terminal? Additionally, is it possible to set the classpath using terminal, and if so, how?

  2. #2
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Compiling and Running Java Programs in Terminal

    You can install the open Java JDK by opening a terminal and running:

    Code:
    sudo apt-get install openjdk-6-jdk
    this will also setup the classpath for you. Now you can simple compile and run you java source with the normal javac / java commands. Lets assume your file is called Myprog.java (with the class called Myprog) and is in your home folder... you can compile with:

    Code:
     javac Myprog.java
    Then run it with:

    Code:
     java Myprog
    Last edited by KdotJ; January 7th, 2012 at 03:53 AM. Reason: typos
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

  3. #3
    Join Date
    Jan 2012
    Beans
    25

    Re: Compiling and Running Java Programs in Terminal

    Quote Originally Posted by KdotJ View Post
    You can install the open Java JDK by opening a terminal and running:

    Code:
    sudo apt-get install openjdk-6-jdk
    this will also setup the classpath for you. Now you can simple compile and run you java source with the normal javac / java commands. Lets assume your file is called Myprog.java (with the class called Myprog) and is in your home folder... you can compile with:

    Code:
     javac Myprog.java
    Then run it with:

    Code:
     java Myprog
    I appreciate your help, but I have already installed openjdk-6-jdk. I appreciate the assistance with the compiler command as well {my computer now attempts to compile the program}, but I need to connect my Java program to a number of classes in a .jar file to run them and I have not yet found a means by which I can set the classpath without error messages.

  4. #4
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Compiling and Running Java Programs in Terminal

    Hey, sorry for the quick reply I was doing it on my phone. So what are the issues you are having? You can compile java source files from the command line but you need to compile and run them with links to jars?

    You can compile with the class path like this (assuming again you're in the correct directory):

    Code:
    javac -cp /path/to/file.jar Myprog.java
    If you have multiple classpaths, and/or in separate locations you can separate them with a colon... such as:

    Code:
    javac -cp /path/to/file1.jar:/path/to/file2.jar Myprog.java
    Once compiled, you can run the java program while including the classpath:

    Code:
    java -cp /path/to/file.jar Myprog
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

  5. #5
    Join Date
    Jan 2012
    Beans
    25

    Re: Compiling and Running Java Programs in Terminal

    I tried to set the classpath as instructed; however the program still didn't run. I got a very long list of error messages because the class wasn't recognized {and therefore neither were its methods}.

  6. #6
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Compiling and Running Java Programs in Terminal

    Is the class (or classes) you're trying to compile and run in a package? Are you in the correct folder etc? If possible, can you post the error output you get? (Of course only if you're OK with showing the class names etc)
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

  7. #7
    Join Date
    Dec 2007
    Location
    Gainesville, Florida
    Beans
    Hidden!
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: Compiling and Running Java Programs in Terminal

    if you are trying to create a jar file from multiple .java files then try

    Code:
    javac *.java MyProg
    from the directory containing the java files.

    For more complex programs I'd suggest an IDE like Eclipse or NetBeans.

    EDIT: I looked up DrJava and it's a java program. You could just download the jar and run it in Ubuntu from the terminal or create a menu item to run it for you.
    I ran it on my computer with
    Code:
    java -jar drjava-stable-20100913-r5387.jar
    Last edited by pbrane; January 7th, 2012 at 10:22 PM.

  8. #8
    Join Date
    Jan 2012
    Beans
    25

    Re: Compiling and Running Java Programs in Terminal

    Thanks!

  9. #9
    Join Date
    Mar 2010
    Beans
    23

    Arrow Re: Compiling and Running Java Programs in Terminal

    Thanks
    Now i can do it from my console with NANO + JAVAC
    No need for GUI

  10. #10
    Join Date
    Apr 2012
    Beans
    3

    Re: Compiling and Running Java Programs in Terminal

    Quote Originally Posted by KdotJ View Post
    You can install the open Java JDK by opening a terminal and running:

    Code:
    sudo apt-get install openjdk-6-jdk
    this will also setup the classpath for you. Now you can simple compile and run you java source with the normal javac / java commands. Lets assume your file is called Myprog.java (with the class called Myprog) and is in your home folder... you can compile with:

    Code:
     javac Myprog.java
    Then run it with:

    Code:
     java Myprog
    Thank you! Works great!!!!!

Page 1 of 2 12 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
  •