Read in file name + path using command line arguments

Hi guys,
I am trying to use command line arguments to read in the name of two files. The code below works as long as the files are on the class path.
public class cmdLineArguments {
     public static void main(String[] args) {
          String xmlFile1 = args[0];
                String xmlFile2 = args[1];
          System.out.println ("\nXmlFile1: " + args[0] + "\nXmlFile2: " + args[1]);
}I would like to be able to read files that are not on the class path as well by entering their full path along with their names.
In other words, It works if I type:
C:\Documents and Settings\user\workspace\CmdLineArgs\src>java cmdLineArgs file1.xml file2.xmlbut not if I type:
C:\Documents and Settings\user\workspace\CmdLineArgs\src>java cmdLineArgs c:\Documents and Settings\user\desktop\file1.xml c:\Documents and Settings\user\desktop\file2.xmlWould anyone please explain how I can edit the code to make it work? I guess I could take them both as a String, modify them manually (add a \ to it etc) but is there an easier way to do it? Even a reference to a tutorial, article etc about this would be very much appreciated.
Also, any idea why a code would compile and run fine in Eclipse but would create an error from command line?
Thanks a lot.

You need to learn about cmd.exe which is used to process the command line. The arguments passed to any program is the command line split at white space so your command line arguments of
c:\Documents and Settings\user\desktop\file1.xml c:\Documents and Settings\user\desktop\file2.xmlis turned into an array of
"c:\Documents", "and", "Settings\user\desktop\file1.xml", "c:\Documents", "and", "Settings\user\desktop\file2.xml"In other words instead of 2 arguments you actually have 6.
To get round this the file names need to be quoted
java cmdLineArgs "c:\Documents and Settings\user\desktop\file1.xml" "c:\Documents and Settings\user\desktop\file2.xml"

Similar Messages

  • Is there a way to pass archive file name to the command-line control?

    Hi there,
    I have been researching the flash media live encoder laterly. But still cannot find confirmed anwsers on the following questions:
    1. Is there a way to pass an archive file name to the command-line control when starting an encoder?
    2. Can the username/password to the Flash Media Server be saved in the profile? If not, is there any otherway to provide(GUI mode) them automatically,
        like as variables to a script?
    Thanks a lot in advance.
    Helen

    1. The only way to inform the command-line encoder about the archive file name is to save the FMLE session profile with the file name. The node for saving the file name is <output><file><path>C:\sample.flv</output></file></path>. The easier way to update the xml profile is to use "Save Profile" option of the GUI mode. If you want to programmatically update the file name, read the profile and update the node before launching the encoder. I hope this answers your question.
    2. No. Credentials can't be saved in the profile. The only way to provide them programmatically is to use /ap and /ab commands for the command-line encoder. For GUI mode, there is no programmable option. But still you can try capturing keystrokes.

  • Can an Access 2013 accdb file be encrypted using command line switches?

    Can an Access 2013 accdb file be encrypted using command line switches?
    I found all the command switches, but nothing about encrypting.  And I've searched online and found nothing.
    Can this be done?
    Thanks!

    Hi Sharon2000,
    I think you could try the Database.NewPassword Method (DAO) in the VBScript file, and you could get more information about it from the link below:
    #Database.NewPassword Method (DAO)
    https://msdn.microsoft.com/en-us/library/office/ff844754.aspx
    Here is a simple demo to achieve your issue.
    Best Regards,
    Edward
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

  • Possible to use command line argument in project file?

    In my CI build process I would like to have a command line argument "customer" that should be used in the project file to include customer related files into the project like this:
    <Content Include="..\customers\%(customer)\*">
      <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
    </Content>
    Is that possible?

    Hi pkursawe,
    Try to use the Copy task, topy the custom files into the project, then add these files into the ItemGroup, check this blog post:
    How To: Recursively Copy Files Using the <Copy> Task
    The wildcard will help you to add related files into the project.
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Deploy EAR file into WAS using command line

    hi,
       I have an EAR file and i want to deploy it into WAS using command line interface.
      I have seen the SDM_commandline.doc file in SDM directory ..it talks about deploying SDA's and SCA's :-
    >   sdm deploy file=<...>
    however it is not working for EAR files...is there any way to deploy EAR files directly or should I convert the EAR to SDA and then deploy using this command.
    thanks

    For NW Java releases prior to 7.1, an EAR file needs to be transformed into a SDA file, then it can be deployed. For later releases, an EAR file can be directly deployed.

  • Prevent Startup Form from Loading using Command Line Arguments

    Hi there
    I'm developing a VB.NET windows forms application in VS2013.
    The application will have 2 ways of being executed, one being by the user manually and the other being automated by the task scheduler.
    When executed by the task scheduler, command line arguments will be used so that the application is aware that it is being run automatically and not by a user.
    My problem is that I want the startup form to load when run by a user but I don't want it to load when it is run automatically, I just want to execute code.
    Is it possible to prevent the startup form from loading by using the commandline arguments from the MyApplication_Startup event?
    Cheers

    Hi Aureuz,
    >>Is it possible to prevent the startup form from loading by using the commandline arguments from the MyApplication_Startup event
    It is impossible to accomplish it in the MyApplication_Startup event, we can use Form.Shown event:
    #Form.Shown Event
    http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown(v=vs.110).aspx
    If we start a form by using the commandline arguments, the count of CommandLineArgs will be above one, if so, use Me.Hide() method to hide this form.
    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
    Dim args As String() = Environment.GetCommandLineArgs()
    If args.Count > 1 Then
    Me.Hide()
    Else
    Me.Show()
    End If
    End Sub
    You can also uncheck "Enable application framework" on the project properties application tab and select your startup object to a module object:
    Module Module1
    Sub Main()
    Dim args As String() = Environment.GetCommandLineArgs()
    If args.Count > 1 Then
    'Run your background code
    Else
    Dim f As New Form1
    f.ShowDialog()
    End If
    End Sub
    End Module
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Using Command Line Arguments with JAVA

    Hi,
    we are using an app called ImageMagick to scale and modify images before they are uploaded to a webserver.
    ImageMagick is called from our Objectiv-C app via
    command line.
    is there a way to this in Java?
    Regards
    Eckbert

    So, ur question is not developing a Java application that
    will do the 'image conversion'; but, your message
    conveys that u want to know how to invoke a Java program built by 'imagemagick' that takes command line arguments?
    Am I right?
    If that's the case, u must look into the documentation
    given along with the download'ed piece of software
    for identifying the 'starting point of the program'. i.e., the
    class that contains the 'main' method! And after you
    identify that class [Eg. ImageMagickStart] then you'll
    have to invoke as follows:
    [you must have Java installed in ur system!]
    c:>java ImageMagickStart
    If the program was written intelligently it must print all
    the command-line arguments required by the above
    program by executing the above command, and from that
    you can identify!
    -RK

  • Converting Doc files to PDF using command line ?

    Hello,
    I need to automate the process of converting (in command line) word files to pdf.
    Is Acrobat distiller Server the right product ? or is there another product from Adobe that would allow me to reach this goal ?
    NB : currently using pdfcreator in command line but have some stability problems now...
    thanks a lot
    Florent

    Acrobat Distiller Server can only convert Postscript to PDF. You may look at this product:
    http://www.adobe.com/products/livecycle/pdfgenerator/

  • UDF to read a file name and use some of its contents to map fields in target.

    I have a scenario where I need to take the month and year from the file name and map it to suitable fields in the target. Ex - filename_apr2014 from this i should be able to read the month and year and map it. How to do this? I tried the udf -
    public String getFileName(Container container){
    DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
    String ourSourceFileName = conf.get(key);
    return ourSourceFileName;
    got this error while testing the mapping -
    Runtime exception when processing target-field mapping /ns0:MT_<<>>_Output/I<<>>; root message: Exception:[java.lang.NullPointerException: while trying to invoke the method com.sap.aii.mapping.api.DynamicConfiguration.get(com.sap.aii.mapping.api.DynamicConfigurationKey) of a null object loaded from a local variable at slot 2] in class <<>> method getSourceFileName[com.sap.aii.mappingtool.tf7.rt.Context@5b04fc0f]
    See error logs for details  

    Hi Satish - You declared "conf" twice.
    DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    UDF -
    String filename    = "";
    DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
    DynamicConfigurationKey key = DynamicConfigurationKey.create( "http:/"+"/sap.com/xi/XI/System/File","FileName");
    filename = conf.get(key);
    return filename;
    And did you define the variable slot 2 in any of your UDF's?
    The above UDF return the filename and you should handle the sub string later..

  • Filenames as Command Line Arguments: How?

    i need to create a program that involves taking in a file name as command line argument, reads the file and then displays its contents on the screen. it needs to work with any file with a .txt extension.
    The only thing i dont fully understand is how to read the file name as a command line argument e.g.
    "java FileReaderApp myfile.txt
    ----contents of file--------
    Thanks in advance
    Carl
    P.S. I have lready read the various helps, utils etc for java and still dont really understand

    Okay, first, it will help if you don't think of "filenames as command line args." The two concepts are totally independent.
    Command line args are Strings. What you do with those Strings is up to you. Processing the command line args is the same, regardless of whether they represent file names or the turnons of the last 50 playmates.
    The args array below contains the command line args.
    public static void main(String[] args) {
    // do stuff
    } Files are generally referred to using the java.io.File. It takes a String for the file name. It doesn't matter where the String comes from.
    Look at FileReader and BufferedReader to actually read the contents of the file.
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileReader.html
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html
    For the basic command line arg stuff, one of the following should have some info and examples
    http://java.sun.com/docs/books/tutorial/
    http://java.sun.com/learning/new2java/index.html
    http://javaalmanac.com
    http://www.jguru.com
    http://www.javaranch.com
    Bruce Eckel's Thinking in Java
    Joshua Bloch's Effective Java
    Bert Bates and Kathy Sierra's Head First Java

  • Copy full file path+file name in Midnight Commander

    Hi!
    How can I copy a file's full path with the file name in Midnight Commander?
    If I press Ctrl+Alt+Enter it's shows this to me in the command line but I can't copy this route.
    If I mark a file with 'Insert' and than if I would like to paste this for example in Mousepad or any other text editor with Shift+Insert or Ctrl+V it isn't works.
    (Relatively newbie sitting here so if the answer is complicated please write a step-by-step instruction )

    Well,I did a lot of research on the internet and I found the best solution.
    - choose the file,press 'Ctrl' + 'Shift' + 'Enter'
    - after that you can see the full file path + the file name in the command line (bottom of the screen)
    - Select the text with 'Shift' + 'Mouse Left Click' (drag it across the whole command)
    - For copy the text, press 'Ctrl' + 'Insert'
    - For paste, press 'Shift' + 'Insert'
    This is still pretty damn slow,I wanted to do it with a single key combination while the file is selected but it looks impossible.
    But at the same time I have started to use Ranger file manager which is also runs from terminal,extremely fast,easy to use and if I press 'Ctrl' + 'Insert' on a file it is copies the full path with the name of the file itself. Just perfect.
    I won't mark the thread [SOLVED] because if anybody knows a better method for this in Midnight Commander (with a script or whatnot) maybe will write it down for us. I hope...

  • How to open url in acrobat reader using command line

    Hi,
    I need to open file from url using command line, but I'm still getting some error (wrong filename).
    I've tried for example this command:
    AcroRd32.exe http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf
    Can anyone help me and tell me what's wrong?

    You can't use a URL as parameter for AcroRd32.exe.

  • Go Online on specific network using command line

    i have a remote troubleshooting software that i wrote myself i have added to it the list of all my clients which can have one of the below:public ipsteamviewer accountslogmein hamachiof course, i'm a paid hamachi client, and i have the latest hamachi version installed whenever i want to troubleshoot any client, i just right click on their name, and select remote desktop.. and the my software will automatically connect to the client using the required authentication/application. when the client has hamachi,his network has to be online, for me to be able to access his ip. so what i'm currently doing is: open hamachi, scroll to my client's network, right click, go online, and then back to my software and troubleshoot! (this is a bit time consuming compared to the other solutions) is there any way, i can go online on a specific network in my local hamachi installation? it would be really a great feature, if we can use a command line script, or use command line arguments to launch special events (locally)example:hamachi.exe /online:mynetworkidhamachi.exe /offline:mynetworkid i appreciate any feedback on this, regards,Dan

    Yes, It would be very interesting to have such a feature.E.g., I need to programmatically request Hamachi to GoOnline, because it often goes offline in some circumstances (after computer wakes from sleep mode for example).

  • Can I convert Word 2003 to PDF using Command Line

    Hello All,
    We have a document generation tool which generates documents in Word 2003 format. The document generation is initiated by the user and the process of generating document is on a remote server. The word file is sent back to the user in word format.
    We would like to convert this doc file to PDF/A format. And we would like to do it programatically, may be using command line arguments. Is it possible?
    Realistically, it is not possible that all the users will have Adobe writer installed in their desktops. In this case, it is possible that Adobe writer is installed in a server and the documents can be converted remotely using command lines?
    Your support on this will be very helpful for us.
    Thanks & Regards
    Irfan

    > Can I use Acrobat Pro to convert a Word 2003 doc to PDF using the Windows command line?
    No.

  • Problem associated with Command Line arguments

    hi ,
    I am not able to pass a file path as command line argument in java.
    Actually i want to run a java program from the command prompt passing a file path . Something like this i tried to do , but its not working.
    D:\VISHAL\src> java CopyFile C:\Documents and Settings\Administrator\Desktop\link.txt
    where ,
    CopyFile is the name of my class which i am going to run
    C:\Documents and Settings\Administrator\Desktop\link.txt is the argument.
    Can anyone tell me whether a java program accepts an argument like this, if yes please tell me the exact syntax..
    Thanks
    Vishal J
    Message was edited by:
    vishal_vj

    is there any other way of solving this?Y don't U try it out with the logic as mentioned
    below it wud be the case if U r not going with
    java CopyFile "C:\Documents and
    Settings\Administrator\Desktop\link.txt" 1
    String filepath = new String();
    for(int i = 0; i < Arg.length; i++)
    filepath.append(Arg);
    File f = new File(filepath);
    What????  This is a non-solution to a non-problem.  First of all, you would be constructing a filename without spaces, when the real path contains spaces.  Secondly, even if you did insert spaces it would not matter.  What if the String (and it does not have to be a filename) contained tabs rather than spaces.  The real (and really only) solution to the problem is to put quotes around the argument, for the exact reason stated in my last post.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Maybe you are looking for