How to pass arguments to a java program.

I want to pass a file name to java program while it is going to execute.
ex: c:\>java <.class> <filename>
so that i have to take that filename into a string. Is it possible? If it is possible please help me.
and how can i check whether the filename argument is given or not through command line argument?

public class Program{
public static void main(String[] args){
filename = args[0];
static String filename;
this code is working. But if I did not give filename, and I only give like this
java Program
Then how can i now whether the argument (args[0]) is passed or not?

Similar Messages

  • How to pass argument to the Java Plugin JVM w/o using the Control Panel?

    I want to deploy an applet to be loaded by the Java Plug In
    and fix some settings of its Java Virtual Machine.
    The JPI Control Panel offers two ways to pass arguments to the JVM,
    none satisfactory.
    1. while interactive via the Control Panel Window.
    This cannot be a solution for a deployed applet.
    or
    2. by editing the system generated file that stores
    the settings of the Plugin Control Panel, using a property
    named javaplugin.jre.params.
    The problem with this method is that if forces to access
    and edit this property file which is stored at various locations
    depending the client platform. Then, it may collide with other
    settings for other applets.
    Is there a way to pass the arguments to the JVM
    from within the html file?
    Has anyone found a solution to this question?
    JPS

    I am interested in this issue as well.
    Did anyone find a reliable way to specify the runtime parameters that should be used by the Java Plug-in in order to execute a given Java applet?
    I believe a good place to specify these runtime parameters would be the applet's JAR manifest: only digitally signed applets should be able to set the desired runtime parameters...
    Any comments / suggestion would be greatly appreciated.
    Regards,
    Marco.

  • How to read system eventlog using java program in windows?

    How to read system eventlog using java program in windows?
    is there any java class available to do this ? or any one having sample code for this?
    Your friend Zoe

    Hi,
    There is no java class for reading event log in windows, so we can do one thing we can use windows system 32 VBS script to read the system log .
    The output of this command can be read using java program....
    we can use java exec for executing this system32 vbs script.
    use the below program and pass the command "eventquery"
    plz refer cscript,wscript
    import java.io.*;
    public class CmdExec {
    public static void main(String argv[]) {
    try {
    String line;
    Process p = Runtime.getRuntime().exec("Command");
    BufferedReader input =
    new BufferedReader
    (new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
    System.out.println(line);
    input.close();
    catch (Exception err) {
    err.printStackTrace();
    This sample program will list all the system log information....
    Zoe

  • How to change system time through java program

    Hi
    I want to know, how to change system time through java program.
    give me a idia with example.
    Thanks

    There isn't any core Java API for this. Use JNI or call an external process with Runtime.exec().
    ~

  • How to pass argument in main function ?

    How to pass arguments in main function of one class from another class ?
    I don't want to pass argument from command prompt.
    I want to try something like this -
    class Test{
    public static void main(String args[]){
    for(int i=0;i<args.length;i++)
         System.out.println(args);
    class Fun{
    public static void main(String args[]){
         Test t=new Test("Hello","good bye");
    when i run Fun class it gives error.
    Suggest me how can i do that.

    In Fun class main method does not take arguments that is fine. In Test class instead of main method you can have constructor to take input parameters as suggested by BalusC
    However, if you want to make your existing code work, you can call (though not appropriate) main() method of Test class from main() method of Fun class (As main() method is static object is not required to invoke this):
    Test.main(new String[]{"Hello","good bye"});Here is your code:
    class Test{
    public static void main(String args[]){
    for(int i=0;i<args.length;i++)
    System.out.println(args);
    public class Fun{
    public static void main(String args[]){
    //Test t=new Test("Hello","good bye");
    Test.main(new String[]{"Hello","good bye"});
    } Thanks,
    Mrityunjoy                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • How to read system evenlog using java program in windows

    How to read system evenlog using java program in windows???
    is there any java class available to do this ? or any one having sample code for this?
    Your friend Zoe

    Welcome to the Sun forums.
    >
    How to read system evenlog using java program in windows???>
    JNI. (No.)
    >
    is there any java class available to do this ? or any one having sample code for this?>You will generally get better help around here if you read the documentation, try some sample code and come back with a specific question (hopefully with an SSCCE included).
    >
    Your friend Zoe>(raised eyebrow) Thank you for sharing that with us.
    Note also that one '?' denotes a question, while 2 or more generally denotes a dweeb.

  • Help--How can I open only one java program at one time?

    How can I open only one java program(same program) in Windows at one time?

    In Java 1.5, you can use the JVM's own monitoring APIs to examine what other JVMs are running on the system, and what applications they're running.
    It's general and powerful, but complex. The socket/file/whatever approach is cleaner, and probably more suited to your usage.
    Don't bother trying to use the Windows task manager for this sort of thing. You have to write messy native code, and it isn't reliable after all that anyway.

  • How to pass arguments from PAPI to the process

    Can any one tell me How to pass arguments from PAPI to the process.

    The link Creating a new work item instance in a process using PAPI shows how to create instances on PAPI and pass in the variable information as they are being created.
    Provide some additional detail if you're interested in seeing how to pass in variable information using PAPI for scenarios other than instance creation.
    Dan

  • How to read e-mail via java programming...

    String host= "myhostname";
    String user="myusername";
    String pass= "mypassword";
    Properties props =System.getProperties();
    Session session =Session.getInstance(props,null);
    Store store= session.getStore("imap");
    System.out.println(store);
    store.connect(host,user,pass)
    this is my java program. when i run this i am getting the following error... but i can able to send the mail.
    Exception in thread "main" javax.mail.MessagingException: Connection refused: connect;
    nested exception is:
         java.net.ConnectException: Connection refused: connect
         at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
         at javax.mail.Service.connect(Service.java:288)
         at javax.mail.Service.connect(Service.java:169)
         at com.ebay.trinity.tps.EmailClient.main(EmailClient.java:25)
    Caused by: java.net.ConnectException: Connection refused: connect
         at java.net.PlainSocketImpl.socketConnect(Native Method)
         at java.net.PlainSocketImpl.doConnect(Unknown Source)
         at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
         at java.net.PlainSocketImpl.connect(Unknown Source)
         at java.net.SocksSocketImpl.connect(Unknown Source)
         at java.net.Socket.connect(Unknown Source)
         at java.net.Socket.connect(Unknown Source)
         at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
         at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
         at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
         at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
         at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:538)
         ... 3 more
    what could be the reason.. am i missing anything here. please help me..
    Thanks in advance
    Marimuthu.sp

    this is my java program. when i run this i am getting the following error... but i can able to send the mail.See this thread, its just about 2 days old:
    [http://forums.sun.com/thread.jspa?forumID=54&threadID=5349815]
    Specially see Reply#4
    Thanks!
    P.S.: Its a good practice to search the forum for the problem you are facing before posting, because there a 50% chances that others might also have faced similar problems before.

  • Hi, i am facing problem as to how to pass arguments received by my swf file to an .exe that i have c

    hi, i am facing problem as to how to pass arguments received by my swf file to an .exe that i have created from a C++ project...do i need to tweak the code in the C++ project in some way that it accepts the input from the .swf file?

    Hi there and welcome to our community
    What you need to do is click the link below, then choose the forum most appropriate to the product you are using. In this forum we discuss the operation of the forums themselves.
    http://forums.adobe.com/index.jspa
    Best of luck to you in solving your issue! Rick

  • How to set environment variables using java program

    Hi all
    I want to set environment variables on windows 98/200/xp system, such as path and classpath using a java program.
    How to do this using a java program.
    Any body plz helppppppppp.

    #1 05-02-2003, 07:38 AM
    Goodz13 Join Date: Jan 2002, Posts: 985
    Location: Halifax, NS, Canada
    Reputation:
    Java FAQ's and tutorials
    Java FAQ's
    Path and ClassPath:
    PATH
    In Windows 9x you would set it up in the autoexec.bat file
    ie.
    SET PATH=%PATH%;c:\jdk1.4.2\bin\;
    where c:\jdk1.4.2\ is the path where you installed Java.
    In Windows 2000 and XP
    Right click on My Computer->Properties->Advanced Tab->Environment Variables... Button.
    If you see a PATH in System Variables, click that and edit it. If you don't, you will need to create a new System variable.
    It should look something like this:
    %SystemRoot%\system32;%SystemRoot%;c:\jdk1.4.2\bin\;
    Any querry email me to [email protected]
    Answer by
    Rajasekhar Goli
    DS UNICS Infotech

  • How to uncompress zip files using java program

    hai,
    please give some sample code to decompress the zip file.
    how to uncompress zip files using java program
    thanking you
    arivarasu

    http://developer.java.sun.com/developer/technicalArticles/Programming/PerfTuning/
    Scroll down to 'Compression'

  • How to pass native struct to java

    Hey guys,
    I've got a legacy C++ prog that outputs structs to a binary file & I've got a header file defining that struct.
    I want to read the file and get the structs via java but I've never used any interface between the languages.
    All I've managed to come up in my searches is "how to pass java objects to c", well how do I do the opposite?
    Help appreciated

    I hear you calling me :)
    If this interests you I'll explain the problem from the beginning-
    I already have a C prog which outputs a HUGE binary file (therefore text is probably not a good idea),
    this file consists of a header followed by some number (defined in the header) of a certain struct (consisting of ~20 fields ints, longs, strings...)
    Another C program exists that reads this file. I am currently building some sort of visual interpretation of the data using java.
    As all C code is quite heavy duty [and works like it should :)], and a few other reasons, I do not want to change it.
    That leaves me with an existing file containing binary data I need to read into the Java UI.
    sztejkat > Why use native code?
    I would have really wished I could do this entirely in Java, as for portability - it doesn't have much consideration, just in terms of "clean" programming.
    As you said struct packing is something unpredictable, therefore I do not see a way of reading one without using C.
    bschauwejava > I suggest you try to be less vague about the results you are getting.
    Sorry :)
    This is the structure: File (package) -> File.java, InsInfo.java (classes)
    I'm using "javac File.java"
    And I'm getting:
    File.java:3: cannot resolve symbol
    symbol : class InsInfo
    location: package File
    import File.InsInfo;
    ^
    File.java:20: cannot resolve symbol
    symbol : class InsInfo
    location: class File.File
    public native InsInfo readIns();
    ^
    2 errors
    (the arrows actually point to the first letter in the class name)
    If I use "Object" instead of "InsInfo" javac compiles the .class without problems.
    I'm using Jbuilder and inside it the import is OK. When I compile it with Jbuilder everything is OK.
    I actually haven't thought of this until now, I'll try to use the .class Jbuilder produces to generate the header.
    btw- Thanks for getting so involved!!

  • How to pass argument in manifest file

    Hi,
    I want to pass argument to main class in jar. Is there any option in manifest file.

    Hmm..
    Seems that my sentence above isn't so correct.
    It possible to add custom attribute: value pairs into manifest file and
    read it later from Java class.
    Here is code sample:
    public class ManifestTest {
        public static void main(String[] args) {
            try {
                java.util.jar.JarFile jar = new java.util.jar.JarFile(System.getProperty("java.class.path"));
                for (java.util.Iterator it = jar.getManifest().getMainAttributes().keySet().iterator();it.hasNext();) {
                    Object curKey = it.next();
                    System.out.println("key: " + curKey + ", value: " + jar.getManifest().getMainAttributes().get(curKey));
            } catch (Throwable t) {
                System.out.println("exception occured: " + t);
    }

  • Passing Japanese characters to Java program

    Hi,
    I am using Java POI APIs for creating/modifying excel spreadsheets. The platform is Solaris. The data to be populated contains both English as well as Japanese characters. The input data to Java program comes from a perl program. When I populate the data in excel or even print using System.out.println, it prints ???... for Japanese characters. The data is in SJIS format.
    I tried converting it to UNICODE in java but that doesn't seem to be working.
    I also tried using native2ascii for converting the data to UNICODE before it is fed to Java program using the following command
    native2ascii -encoding SJIS <Input File> <Output File>
    Although it converts the charaters into UNICODE correctly in the form \u8a3c\u523... but when it is input to Java, the program prints the string as such.
    But if this string is hardcoded in the program, the Japanese characters are printed correctly.
    Could anybody please throw some light as to the data should actually be passed to the Java program.
    Thanks

    Sekhar
    Ive realised the solution will probably involve HttpServletRequest.setCharacterEncoding.
    Im now upgrading to Tomcat 4 because 3 didnt support this method. Once I'm through the upgrade, I'll try Japanese the chars again.
    I would guess I need to force my web pages to utf-8 and use HttpServletRequest.setCharacterEncoding(utf-8) in the servlet to get it working ?

Maybe you are looking for