• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issue in passing command line arguments

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I am having issue with command line arguments below is my program, when I passed arguments like 800PBXMAIN 2 3 4 5 6 7, I just got the out put of these lines

System.out.println ("Incorrect number of errors!");
System.out.println ("Usage: java PBXMain filename");
System.exit(0);

While the program runs properly and dedugging is also with no issue.

package com.pbx.main;

import java.net.*;
import java.io.*;
import com.pbx.core.*;
import java.util.*;


public class PBXMain {

/**
* Properties file
*/
private static final String CONFIG_BUNDLE_NAME = "properties.ApplicationConfigurations";

/**
* Properties file
*/
private static final String CONFIG_BUNDLE_NAME2 = "properties.PBXLogging";


/**
* For holding the PropertyResourceBundle
*/
private static PropertyResourceBundle configBundle;




static {
try {
configBundle = (PropertyResourceBundle) PropertyResourceBundle.
getBundle(CONFIG_BUNDLE_NAME);

} catch (Exception ex) {

ex.printStackTrace();
}
}

/**
* jarFilePath
*/
private static String jarFilePath = configBundle.getString("800PBXMAIN");


public PBXMain() {

}

public static void main(String[] args) {
PBXMain pbxmain = new PBXMain();

Call call =null;

if (args.length >= 0) {
System.out.println ("Incorrect number of errors!");
System.out.println ("Usage: java PBXMain filename");
System.exit(0);
}



int startint = args[0].indexOf("/");

String jarFile = args[0].trim() + ".jar";

int endint = args[0].indexOf("_");

args[0] = args[0].substring(startint + 1 , endint);

String mainClass = args[0].trim();

String iDNum =args[1].trim();

String itemID =args[2].trim();

String pinNO =args[3].trim();

String pinNO1 =args[4].trim();

String pinNO2 =args[5].trim();

String pinNO3 =args[6].trim();




try {
// Load all classes from jar file without giving classpath
URL url = new URL("jar:file:" + jarFilePath + "/" + jarFile + "!/");

JarURLConnection jarConnection = (JarURLConnection) url.openConnection();

URL[] urls = new URL[] {url};

ClassLoader classLoader = new URLClassLoader(urls);

CallHandler callobject =(CallHandler) (classLoader.loadClass(mainClass).newInstance());

try{ call = new Call();

} catch (Exception ex){

ex.printStackTrace();
}

try{
callobject.answerCall(call,iDNum,itemID,pinNO,pinNO1,pinNO2,pinNO3);
} catch (Exception ex){

ex.printStackTrace();
}


} catch (InstantiationException ie) {
ie.printStackTrace();
/*

( "Raised InstantiationException while reading protocol. " +
ie.getMessage());
*/

} catch (ClassCastException cce) {
cce.printStackTrace();

/*
( "Raised ClassCastException while reading protocol. " +
cce.getMessage());
*/

} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();

/* ("Raised ClassNotFoundException while reading protocol. " +
cnfe.getMessage());
*/
}

catch (FileNotFoundException fe) {
fe.printStackTrace();

/* ("Raised FileNotFoundException while reading protocol. " +
fe.getMessage());
*/

} catch (SecurityException se) {
se.printStackTrace();

/* ("Raised SecurityException while reading protocol. " +
se.getMessage());
*/

} catch (IOException ioe) {
ioe.printStackTrace();

/* ("Raised IOException while reading protocol. " +
ioe.getMessage());
*/

} catch (Exception e) {
e.printStackTrace();

/* ("Raised Exception while reading protocol. " +
e.getMessage());
*/
}

}
}
Please help me this.

Thanks & Regards

Ajay Sngh Yaduwanshi
 
ajay yaduwanshi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for confusion.

Even after putting Sytem.out.println("args[1]" + iDNum);

I am not getting the command line argument printed in the console..

Thanks & Regards

Ajay Singh Yaduwanshi
ajay.yaduwanshi@gmail.com
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to send in arguments but instead get a exit?
Thats because your if case is wrong, you write :


You should write:



More safer check would be cheking every time you take out a argument that you really have one or check that you have exactly the correct amount of arguments.
 
ajay yaduwanshi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding. Even after puttng the if condition as said by you i.e.
if(args == 0)I am only getting the same out out i.e. Incorrect number of errors!
Usage: java PBXMain filename

I already said abt the arguments I am providing.

I am passing the arguments through net beans arguments block which we will get right click on the file then choosing properties then Run and the putting the arguments in the arguments field..

Even if I put in the if condition if(args! =1)
I got the same output.

Thanks & Regards

Ajay Singh Yaduwanshi
 
marko salonen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, the problem is that you dont get any arguments to your program.

But the logic in the program is still wrong if you write args.length >= 0
because this line of code allways returns true.

Problem is not in code, its in how you initliaze the code and that i cant give any asnwers to because im not common to that tool.
 
ajay yaduwanshi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying. I am really grateful that you spend time in my queries and gives your best to solve it.

Best
Ajay Singh Yaduwanshi
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic