Solved

NumberFormat Exception in Batch File but not Eclipse

Posted on 2011-03-01
23
736 Views
Last Modified: 2012-05-11


Hello,

I'm not sure how to solve this issue.  The code in Eclipse is running but the batch file throws a number format exception: null

The issue is at the Integer.parseInt.

missingValue = getMissingValueInteger();

 private static int getMissingValueInteger()
    {
        String missingValueProperty = System.getProperty("missingValue");
        int parseIntegerMissingValue = Integer.parseInt(missingValueProperty);
        missingValue = parseIntegerMissingValue;
        return missingValue;
    }

Open in new window

0
Comment
Question by:AndyC1000
  • 8
  • 6
  • 5
  • +3
23 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35014606
looks like the system property is not being set
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35014607

Add
System.out.println("miissinvalue: " + missingValueProperty);
just before
 int parseIntegerMissingValue = Integer.parseInt(missingValueProperty);
       
0
 

Author Comment

by:AndyC1000
ID: 35014654
I debugged the code and it displayed the correct info in Eclipse.

I added an echo in the batch file it returned -DmissingValue=-1 which is the value I passed in using:
SET MISSING_VALUE=-DmissingValue=-1

then:

java  -classpath bin;lib/log4j-1.2.14.jar;lib/commons-collections.jar;commons-lang-2.5.jar;lib/velocity-1.6.4.jar;lib/xercesImpl.jar;lib/castor-0.9.5.2.jar;.\lib <package name>.<main class> %INPUT_DIR% %OUTPUT_DIR% %OUTPUT_FILENAME% %MISSING_VALUE% %LOGFILE%

0
 

Author Comment

by:AndyC1000
ID: 35014655
I meant to write it displayed the correct value.
0
 

Author Comment

by:AndyC1000
ID: 35014753
Could it be a missing library to do with number parsing?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35014776

But you are saying that when you run it

java -DmissingValue="-1"  class_name

it actually works? That seems reasonable looking at the code.
Then what is the issue?
0
 
LVL 92

Expert Comment

by:objects
ID: 35014802
try this:

java  -classpath bin;lib/log4j-1.2.14.jar;lib/commons-collections.jar;commons-lang-2.5.jar;lib/velocity-1.6.4.jar;lib/xercesImpl.jar;lib/castor-0.9.5.2.jar;.\lib %MISSING_VALUE% <package name>.<main class> %INPUT_DIR% %OUTPUT_DIR% %OUTPUT_FILENAME%  %LOGFILE%
0
 

Author Comment

by:AndyC1000
ID: 35014810
The batch file throws a number format exception at:

missingValue = getMissingValueInteger();

 private static int getMissingValueInteger()
    {
        String missingValueProperty = System.getProperty("missingValue");
        int parseIntegerMissingValue = Integer.parseInt(missingValueProperty); <---- here
        missingValue = parseIntegerMissingValue;
        return missingValue;
    }

Open in new window


The strange thing is it works in Eclipse, and I've debugged the method and the correct value is returned.
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 35014829
I think its comes with sepace! jsut trim that !!
0
 

Author Comment

by:AndyC1000
ID: 35015108
objects and dravidnsr I used both your suggestions and it worked - there are no errors and the output file is created.

The logging component isn't working when running the batch file.  I've checked the classpath and the jar exists. -classpath bin;lib/log4j-1.2.14.jar;  Any suggestions?

I'm using org.apache.log4j.Logger;

::// Batch File 

@ECHO ON
ECHO STARTED Batch..

if not "%JAVA_HOME%" == "" goto javaHomeAlreadySet
for %%P in (%PATH%) do if exist %%P\java.exe set JAVA_HOME=%%P..\
:javaHomeAlreadySet

:: Directory and file locations
SET INPUT_DIR=data/adaptor/input/State
SET OUTPUT_DIR=data/adaptor/output
SET OUTPUT_FILENAME=output
SET MISSING_VALUE=-DmissingValue=-1 
SET LOGFILE=-DlogFile_var=data\adaptor\logger

:: Date for log file
FOR /F "tokens=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
FOR /F "tokens=5 delims= " %%A in ('echo ^| time ^| find "current" ') DO FOR %%B IN (%%A) DO SET Now=%%B
FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%") DO (
    SET Day=%%A
    SET Month=%%B
    SET Year=%%C
)
FOR /F "tokens=1-3 delims=:.-" %%A IN ("%Now%") DO (
    SET Hour=%%A
    SET Min=%%B
    SET Sec=%%C
)
:: Log file prefix
SET LOGDIR=%Year%-%Month%-%Day%_%Hour%%Min%%Sec%
:: Read and convert output

@ECHO ON

ECHO STARTING 


:: How to run program here-->
java  -classpath bin;lib/log4j-1.2.14.jar;lib/commons-collections.jar;commons-lang-2.5.jar;lib/velocity-1.6.4.jar;lib/xercesImpl.jar;lib/castor-0.9.5.2.jar;.\lib %MISSING_VALUE% <package>.<main class> %INPUT_DIR% %OUTPUT_DIR% %OUTPUT_FILENAME% %LOGFILE%


ECHO FINISHED READING and converting output
::ECHO File %OUTPUTDIR%%OUTPUT_FILENAME%.xml has been created.

pause

:END
ECHO ...End Batch
@ECHO ON

Open in new window





0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35016001
>>
:: How to run program here-->
java  -classpath bin;lib/log4j-1.2.14.jar;lib/commons-collections.jar;commons-lang-2.5.jar;lib/velocity-1.6.4.jar;lib/xercesImpl.jar;lib/castor-0.9.5.2.jar;.\lib %MISSING_VALUE% <package>.<main class> %INPUT_DIR% %OUTPUT_DIR% %OUTPUT_FILENAME% %LOGFILE%
>>



You're not passing the variables to java properly. You specify them as jvm args but use them as normal program args. You need
java  %MISSING_VALUE% %LOGFILE% -classpath etcetera

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 86

Expert Comment

by:CEHJ
ID: 35016076
btw, your batch file also seems to reverse the intended meanings of LOGFILE and LOGDIR
0
 
LVL 10

Expert Comment

by:gordon_vt02
ID: 35017100
Where is your log4j.properties/xml file located, or are you configuring log4j programmatically based on the LOGFILE parameter to your app?  Check your config file too.  If it uses relative paths for the log file, it may be showing up somewhere unexpected based on the working directory when you run the batch file.

Also, if you're using JDK 1.6, you can clean up your classpath a little by using wildcards:

java -classpath bin;lib/* ...

That will load any .jar files in the lib directory into the classpath.
0
 

Author Comment

by:AndyC1000
ID: 35022594
I forgot to add the log4j.properties file.

I've inserted log4j.properties into the lib folder and updated the classpath:

java  -classpath bin;lib\*;lib\log4j.properties;.\lib %MISSING_VALUE% <package>.<main class> %INPUT_DIR% %OUTPUT_DIR% %OUTPUT_FILENAME% %LOGFILE%

Open in new window


Although it still doesn't work.

In my properties file I have:
log4j.appender.rollingFile.File = ${logFile_var}/mylog.log

Notice I set the below in the batch:
SET LOGFILE=-DlogFile_var=data\adaptor\logger

Could this be the problem?  The logging worked in the eclipse project.


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35022705
In that case

>>log4j.appender.rollingFile.File = ${logFile_var}/mylog.log

should be more like

>>log4j.appender.rollingFile.File = ${LOGFILE}/mylog.log

(but see my comments above about your log paths)
0
 
LVL 92

Expert Comment

by:objects
ID: 35022728
same problem as I fixed for you earier

command line sjhould be:


java  -classpath bin;lib\*;lib\log4j.properties;.\lib %MISSING_VALUE%  %LOGFILE% <package>.<main class> %INPUT_DIR% %OUTPUT_DIR% %OUTPUT_FILENAME%

only put args for your app after the class, args for the vm (like system properties) go before the class
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 35022754
>>same problem as I fixed for you earier

You didn't unfortunately, since LOGFILE was still in the wrong place
0
 

Author Comment

by:AndyC1000
ID: 35023284
Thanks that worked!

The only problem with my batch is specifying the name of the log file.

:: Date for log file
FOR /F "tokens=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
FOR /F "tokens=5 delims= " %%A in ('echo ^| time ^| find "current" ') DO FOR %%B IN (%%A) DO SET Now=%%B
FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%") DO (
    SET Day=%%A
    SET Month=%%B
    SET Year=%%C
)
FOR /F "tokens=1-3 delims=:.-" %%A IN ("%Now%") DO (
    SET Hour=%%A
    SET Min=%%B
    SET Sec=%%C
)
:: Log file prefix
SET LOG_FILENAME=%Year%-%Month%-%Day%_%Hour%%Min%%Sec%

Then I'm adding to the log4j.properties:
log4j.appender.rollingFile.File = ${LOGDIR}/${LOG_FILENAME}.log

Do I need to insert the argument into eclipse and build/ export again?

Open in new window

0
 

Author Comment

by:AndyC1000
ID: 35023285
Do I need to insert the argument into eclipse and build/ export again?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 35023339
> log4j.appender.rollingFile.File = ${LOGDIR}/${LOG_FILENAME}.log

why did you change the log4j config? What you had before was fine

log4j.appender.rollingFile.File = ${logFile_var}/mylog.log

> Do I need to insert the argument into eclipse and build/ export again?

SHouldn't need to. the only problem was with your batch file
0
 
LVL 92

Expert Comment

by:objects
ID: 35023349
> SET LOG_FILENAME=%Year%-%Month%-%Day%_%Hour%%Min%%Sec%

the rolling file appender will handle naming the file for you.
what you originally had is all you need
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35024947
>>Thanks that worked!

Good - glad i could help. (Posted at http:#35016001 )
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35068074
:)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now