让Java在Cygwin中工作

时间:2014-05-22 15:04:17

标签: java linux cygwin

我刚刚下载了cygwin来运行一些java程序,我需要设置java。我试图下载标准的linux软件包并得到错误Cannot execute binary files

经过一些研究,我认为我必须将它指向我的windows java文件夹,所以我跑了

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

它似乎工作(java -version显示我的java信息)。然而它似乎搞砸了我的linux bash设置,之后我不能做基本的东西,如ls,sh等等,它给命令找不到错误,唯一的办法是将PATH变量改回我的cygwin文件夹。

我怎样才能获得java"安装"在cygwin?

3 个答案:

答案 0 :(得分:1)

首先:

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

第二

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

当你启动一个新的bash shell时,它将回显这四个java要素的版本。 运行每一个没有参数,你应该得到四个适当的投诉。如果它们没有运行,那么您需要安装它们。

然后创建一个名为src的目录并创建HelloWorld.java:

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

在同一目录中,创建HelloWorldAsPackage.java:

package src;

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

然后创建bash脚本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 "=================================================="

然后创建bash脚本runThemAll.bash

#!/bin/bash

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

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

现在运行脚本:在cygwin提示符下键入runThemAll.bash。

如果你得到以下输出,你可以考虑在cygwin下安装java。

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'!

答案 1 :(得分:0)

你不需要cygwin在windows中运行java。您可以设置路径环境变量以启用其使用。既然你已经安装了java,那你就到了一半。

1. Right-click on 'My Computer' and select 'Properties'.(i.e. go to System Properties)
2. Click on the 'Environment variables' button under the 'Advanced' tab.
3. Alter the 'Path' variable so that it also contains the path to the Java executable.
     Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;C:\Program Files (x86)\Java\jdk1.7.0_51\bin'.

完成这些步骤后,您可以从命令提示符运行java,甚至也可以在cygwin中运行java,因为它也使用了系统路径变量。

答案 2 :(得分:0)

哦,哈哈新秀的错误。你必须将java源目录附加到PATH,而不是直接导出它。所以export PATH=$PATH:"/cygdrive/C/Program Files (x86)/Java/jdk1.7.0_51/bin"而不是export PATH="/cygdrive/C/Program Files (x86)/Java/jdk1.7.0_51/bin