getting Java to work in Cygwin

11,316

First:

Within cygwin, use bash.
Also install java8, older versions lack key features.

Second:

Add the following lines to your .bashrc file:
echo "javac     :`which javac`"
echo "jar       :`which jar`"
echo "java      :`which java`"
echo "ant       :`which ant`"

When you start a new bash shell, it will echo the versions of these four java essentials. Run each one with no arguments and you should get four appropriate complaints. If they don't run, then you need to install them.

Then create a directory called src and create HelloWorld.java:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello World from main!");
    }
}

In the same directory, create HelloWorldAsPackage.java:

package src;

public class HelloWorldAsPackage
{
    public static void main(String[] args)
    {
        System.out.println("Hello World In a Package (Directory) called 'src'!");
    }
}

Then create the bash script runOneCase.bash:

#!/bin/bash

echo "Listing $1.java"
echo "=================================================="
cat $1.java
echo "=================================================="
echo "Compiling $1.java"
echo "+ javac $1.java"
javac $1.java
echo "+ cd $2"
cd $2
echo "Running $1.java"
echo "+ java $3$1 $4"
java $3$1 $4
echo "=================================================="

Then create the bash script runThemAll.bash

#!/bin/bash

echo "CASE 1: HelloWorld"
runOneCase.bash HelloWorld . "" ""

echo "CASE 2: HelloWorld as a Package"
runOneCase.bash HelloWorldAsPackage .. src. ""

Now run the script: runThemAll.bash by typing it at the cygwin prompt.

If you get the following output, you can consider java installed under cygwin.

CASE 1: HelloWorld (Hit Return)

Listing HelloWorld.java
==================================================
public class HelloWorld
{
        public static void main(String[] args)
        {
                System.out.println("Hello World from main!");
        }
}
==================================================
Compiling HelloWorld.java
+ javac HelloWorld.java
+ cd .
Running HelloWorld.java
+ java HelloWorld
Hello World from main!
==================================================
CASE 2: HelloWorld as a Package (Hit Return)

Listing HelloWorldAsPackage.java
==================================================
package src;

public class HelloWorldAsPackage
{
        public static void main(String[] args)
        {
                System.out.println("Hello World In a Package (Directory) called 'src'!");
        }
}
==================================================
Compiling HelloWorldAsPackage.java
+ javac HelloWorldAsPackage.java
+ cd ..
Running HelloWorldAsPackage.java
+ java src.HelloWorldAsPackage
Hello World In a Package (Directory) called 'src'!
Share:
11,316
full stackoverflow developer
Author by

full stackoverflow developer

Updated on June 04, 2022

Comments

  • full stackoverflow developer
    full stackoverflow developer about 2 years

    I just downloaded cygwin to run some java programs and i need to set up java. I tried to download the standard linux packages and got the error Cannot execute binary files

    After some research I figured I had to point it to my windows java folder, so I ran

    export PATH=$PATH:"/cygdrive/C/Program Files (x86)/Java/jdk1.7.0_51/bin"
    

    and it seems to work (java -version shows my java info). However it seems to screw up my linux bash settings, afterwards I cant do basic stuff such as ls, sh, etc. it gives command not found error and the only way is to change the PATH variable back to my cygwin folder.

    how can i get java "installed" in cygwin?