java - jar 没有在命令提示符下给出输出,但在 eclipse 中工作正常

标签 java eclipse jar command-prompt executable-jar

我制作了一个调度程序来安排一些任务

public class CronTriggerApp {
@SuppressWarnings("rawtypes")
public static void main(String[] args) throws Exception {

    System.out.println("****************************************************************************");
    System.out.println("*                                                                          *");
    System.out.println("* Note : Please Place Jar and Properties file in same Folder               *");
    System.out.println("*                                                                          *");
    System.out.println("****************************************************************************");

    Properties cronProp = Utility.getPropFileData();

    Set set = cronProp.entrySet();
    Iterator itr = set.iterator();
    while (itr.hasNext()) {
        Map.Entry entry = (Map.Entry) itr.next();
        String propKey = (String)entry.getKey();
        String propValue = (String)entry.getValue();
        String timeArr[] = propValue.split(":");
        String seconds = timeArr[2];
        String minuts = timeArr[1];
        String hour = timeArr[0];

        String cronExp = seconds.trim()+" "+minuts.trim()+" "+hour.trim()+" "+"? "+ "* "+ "MON-FRI";
        System.out.println("Is Valid Time Declaration: "+CronExpression.isValidExpression(cronExp));
        if(CronExpression.isValidExpression(cronExp)) {
            JobDataMap map = new JobDataMap();
            map.put("TAG", propKey);

            JobKey jobKey = new JobKey("jobObj"+propKey, "group"+propKey);
            JobDetail jobObj = JobBuilder.newJob(ScheduleTask.class).withIdentity(jobKey).setJobData(map).build();

            //CRON_SCHEDULE = 0 46 11,12 ? * MON-FRI
            Trigger trigger =   TriggerBuilder.newTrigger().withIdentity("TriggerName"+propKey, "group"+propKey)
                                .withSchedule(CronScheduleBuilder.cronSchedule(cronExp)).build();

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();

            scheduler.start();
            scheduler.scheduleJob(jobObj, trigger);

        }else {
            System.out.println("Please Provide Time in HOUR:MINUTS:SECONDS[(0-23):(1-59):(1-59)] Formate for "+propKey);
        }

    }


}

当触发器执行时,将调用下面的类

  public class ScheduleTask implements Job {


    @SuppressWarnings("static-access")
    public void execute(JobExecutionContext arg) throws JobExecutionException{

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Today's Date time : "+dtf.format(now));

        OutputStream stdin = null;  
        BufferedReader stdError = null;
        BufferedReader stdInput = null;

        JobDataMap jobDataObj = arg.getMergedJobDataMap();
        String tag = (String)jobDataObj.get("TAG");
        System.out.println(tag);

        //"OB" "Opening  Bell" "Sensex:45,778,Nifty:75,888"
        String headerAndmessage = SchedulerDto.pshNotification(tag);
        String argumentParam = tag+" "+headerAndmessage;

        Hashtable<String, String> hashTable = new Hashtable<String, String> ();
        hashTable.put("REQDATA",headerAndmessage);
        hashTable.put("ACTION","I");
        String uniqueId = SchedulerDto.reqRespLog(hashTable);


        File file = new File("");
        String filepath = file.getAbsolutePath()+file.separator+"IVLGenericAlerts.jar";
        String cmdwithArg =" java -jar " +filepath+" "+argumentParam ;
        System.out.println("command and jar path detail with database response: "+cmdwithArg);
        try {
            // suppose cmdWithArg  is java -jar D:\\user_workspace\\IVLGenericAlerts.jar 
            **Process process = Runtime.getRuntime().exec(cmdwithArg);**

            stdin = process.getOutputStream ();  

            stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));


            System.out.println("Here is the standard output of the command:");
            String lastLine = null;
            String currentLine = null;
            System.out.println("is output" + (currentLine = stdInput.readLine()) != null);
            while ((currentLine = stdInput.readLine()) != null) {
                lastLine = currentLine;
                System.out.println("abc");
                System.out.println(currentLine);
            }
            System.out.println("before xml");
            String xmlToJson = "<?xml version=\"1.0\" ?>"+lastLine;
            JSONObject xmlJSONObj = XML.toJSONObject(xmlToJson);
            String jsonPrettyPrintString = xmlJSONObj.toString();

            JSONObject jsonObject = new JSONObject(jsonPrettyPrintString);
            System.out.println(jsonObject);
            JSONObject enp = (JSONObject)jsonObject.get("Envelope");
            JSONObject Body = (JSONObject)enp.get("Body");
            JSONObject RESULT = (JSONObject)Body.get("RESULT");
            Object SUCCESS = RESULT.get("SUCCESS");
            Object RecipientId = RESULT.get("RecipientId");
            Object ORGANIZATION_ID = RESULT.get("SUCCESS");


            hashTable.put("ACTION","U");
            Map<String, String> hm = new HashMap<String, String>();
            hm.put("SUCCESS", SUCCESS.toString());
            hm.put("RecipientId", RecipientId.toString());
            hm.put("ORGANIZATION_ID", ORGANIZATION_ID.toString());
            hashTable.put("RESPDATA",hm.toString());
            hashTable.put("UNIQUEID", uniqueId);
            SchedulerDto.reqRespLog(hashTable);

            //System.out.println(hashTable.toString());


            System.out.println("Here is the standard error of the command (if any):\n");
            System.out.println("in error");
            stdError = new BufferedReader(new  InputStreamReader(process.getErrorStream()));
            while ((currentLine = stdError.readLine()) != null) {
                System.out.println(currentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                stdin.close();
                stdError.close();
                stdInput.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }

    }

}

您可以在上面的类 String 变量 cmdWithArg 中看到。当我从 Eclipse 运行它时,这将给出输出,但是当我从 Eclipse 与其他目标生成 jar 时,这将不会给出任何输出,我们正在使用自定义 jar 进行输出 IVLGenericAlerts.jar 。希望你理解

最佳答案

问题已解决,当我们在命令提示符中访问文件夹时,我们只需放置转义双引号“”

关于java - jar 没有在命令提示符下给出输出,但在 eclipse 中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56163884/

相关文章:

java - 如果一个类调用另一个类,我可以将它们放在单独的 JAR 文件中吗?

java - JLists 中 JButton 监听器事件的多个实例

eclipse - Node 食 : Unable to run even the simplest app

java - 使 JComboBox 可编辑而不影响其宽度

c++ - 强制 eclipse 为派生类中的纯虚函数的实现提供 stub

android - 导入openCV总是显示错误

java - Spring 错误 - java.lang.NoSuchMethodError : > org. springframework.beans.factory.annotation.InjectionMetadata.<init>

java - Gradle - 将第三方依赖项安装到本地 Ivy 存储库中

java - 设置 Cassandra 代码格式化程序,options/code.style.schemes.xml 的用途是什么?

java - 用 Java 读取 csv 文件。用数组列表填充数组列表