首页 文章

如何在Windows中使用shell脚本调用在Eclipse IDE中编写的java程序而不知道classpath?

提问于
浏览
0

我需要这个概念的帮助 . 我搜索了这个论坛中的几个主题,但没有一个符合我的问题

我使用Eclipse IDE编写了一个Java类,并将.sh文件保存在同一个文件夹中 . 我使用Git Bash运行shell脚本,因为我没有任何其他Linux终端 .

这个脚本文件也应该在另一台机器上运行这个java程序 . (我必须将这些java文件和shell脚本发送给一个评估器,这样当他在他的机器上运行shell脚本时它就会运行)所以我不知道java和classpath设置将如何在另一台机器上 .

当我调用时,我得到如下错误

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This class creates two instances of same class 
 * and performs message transmission.
 * 
 */
public class App {
    final static String initTurn = "initiator";
    final static String receiverTurn = "receiver";

    public static void main(String[] args) {
        App app = new App();
        InputStreamReader r = new InputStreamReader(System.in); // Input reader for messages
        InputStreamReader totalMessages = new InputStreamReader(System.in); // Input reader to get total number of messages
        BufferedReader br = null;
        Player initiator = new Player(); /* Instance for Player who initiates the message */
        Player receiver = new Player(); /* Instance for player who receives and re-sends the message. */

         / ** some implementations  */
    }
}

public class Player {

    String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

我的shell脚本代码

#!/bin/sh
javac Player.java
javac App.java
java App

当我从Git Bash运行shell脚本时,我收到如下错误 . 所有Java类和脚本文件都在同一个文件夹中 .

App.java:21: error: cannot find symbol
                Player initiator = new Player(); /* Instance for Player who initiates the message */
                ^
  symbol:   class Player
  location: class App
App.java:21: error: cannot find symbol
                Player initiator = new Player(); /* Instance for Player who initiates the message */
                                       ^
  symbol:   class Player
  location: class App
App.java:22: error: cannot find symbol
                Player receiver = new Player(); /* Instance for player who receives and re-sends the message. */
                ^
  symbol:   class Player
  location: class App
App.java:22: error: cannot find symbol
                Player receiver = new Player(); /* Instance for player who receives and re-sends the message. */
                                      ^
  symbol:   class Player
  location: class App
4 errors
Error: Could not find or load main class App

1 回答

  • 0

    您可以在shell脚本中设置当前文件夹的类路径,如下所示:
    export CLASSPATH = $ CLASSPATH: .
    这样它就能在同一个文件夹中找到Player类 .

相关问题