0
votes

Jar generated by Ant script does not support user input class file is

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

public class LogClass {

public static void main(String argsp[]) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(System.in));
    System.out.println(bufferedReader.readLine());
  }

}

build.xml is

<project name="HelloWorld" basedir="." default="main">

<property name="src.dir"     value="src"/>

<property name="build.dir"   value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir"     value="${build.dir}/jar"/>

<property name="main-class"  value="LogClass"/>



<target name="clean">
    <delete dir="${build.dir}"/>
</target>

<target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>

<target name="jar" depends="compile">
    <mkdir dir="${jar.dir}"/>
    <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        <manifest>
            <attribute name="Main-Class" value="${main-class}"/>
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>

<target name="clean-build" depends="clean,jar"/>

<target name="main" depends="clean,run"/>

I am using eclipse luna latest version with JDK 1.8. after running build file jar created successfully but on running jar does not support user input when running on eclipse. Command prompt support user input but it does not support externat liberary. Please help. my project need both user input and external liberary.

2
What do you mean with not support? And why does it not support external Libs, from CommandPrompt ? How do you start the JAR from command prompt? - Ben

2 Answers

1
votes

When you run from the console, you probably use java executable and System.in (which normally represents the console) is available as the console itself is obviously available. When you run from Eclipse, you probably use javaw executable, which runs the graphics-only mode i.e. no console. The reason for external library not working is that you don't have it on the jar's classpath. Modify your manifest to include it, or specify the correct classpath as a commandline argument.

1
votes

The main problem is that you're executing your jar file with fork="true", which causes Ant to run your application in a child process of Ant, thus no console will be avalable for it, nor will the standard input.

I see 2 possible workarounds: either run your application jar with fork="false" or use a GUI dialog to get the input, for instance using JOptionPane, instead of getting input from System.in.