Reading files from within helper classes

From within a servlet, I can get the servlet context to get a path to the "web" directory of my project and easily access properties files for my project. If I create another package in my project to hold helper classes that perform some specific function and want to create and/or read from a properties file, I get a path to the bin directory of my servlet container:
Servlet Path: D:\Documents and Settings\Josh\My Documents\Josh\Projects\ServletSandBox\build\web\
Helper Class File Path: D:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.14\bin\test
I'd like to develop my helper packages as reusable APIs, so they're not tied directly to my project or to a servlet in general, but I'd like the properties files to stay somewhere in my project folder, preferably in the WEB-INF directory with the servlet's own specific properties file.
I can't figure out the best way to handle this. I wanted to just be able to pass the helper class a String if somebody wanted to specify a filename other than the default filename. Does it make sense to specify a file URL instead? Is there any other way to get the calling servlet's context path without having to pass in a variable?
Thanks

It seems that the helper API shouldn't need to be given a path to its own config file. I would like to have code that will load the file regardless of whether or not its in a stand alone java app or a servlet. Whenever the helper API is jarred up and imported into my servlet, it can't find the config file unless it's in the "D:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.14\bin\test" directory. OR, if I use the current thread to get the loader, it will locate it in the "classes" folder, which is the only solution I can come up with that will let me write code to load the file that will work, regardless of its implementation.
The "project" folder I'm talking about is just the NetBeans "project" folder for the servlet, or the "ServetSandBox" folder in my example. I guess that doesn't really matter in the context of the servlet though.
So is using the current thread the appropriate way to do this? When you say that code will work if I put the resource in the WEB-INF directory, that seems to only be true if I try to load it from the servlet itself, not the helper class. The helper class doesn't find the resource in the WEB-INF directory.
Also, I would like to be able to write the default values to a new config file if one doesn't exist. How in the heck do I get a pointer to the "classes" folder, or any other folder that I can also read from using the classloader, so I can write to a new file?

Similar Messages

  • Problem executing .bat file from within Java class

    I'm stumped: I have no problem executing a .bat file that sets classpaths and executes a Java jar, but when I try to execute other executables first and then the .jar, my application hangs and since the DOS box doesn't come up over my GUI, I can't see what's going on.
    This works:
    public void execute() throws Exception {
    String s = "c:\\cs47auxs\\omnijar\\omni.bat";
    Process p = Runtime.getRuntime().exec("\"" + s + "\"");
    p.waitFor();
    JOptionPane.showMessageDialog(frame,
    "The Database Has Been Successfully Reloaded.",
    "Information Message",
    JOptionPane.INFORMATION_MESSAGE);
    Here's the .bat 'omni.bat'
    set JAVA_HOME=c:\j2sdk1.4.2_04\bin
    %JAVA_HOME%\java -jar C:\CS47AUXS\OMNILOADJAR\OmniLoad.jar
    This doesn't work:
    public void execute() throws Exception {
    String s = "c:\\cs47auxs\\omnijar\\jobomni.bat";
    Process p = Runtime.getRuntime().exec("\"" + s + "\"");
    p.waitFor();
    JOptionPane.showMessageDialog(frame,
    "The Database Has Been Successfully Reloaded.",
    "Information Message",
    JOptionPane.INFORMATION_MESSAGE);
    Here's the .bat file 'jobomni.bat'
    SET NETX_HOME=C:\CS47AUXS
    SET COBOL_HOME=C:\CS47AUXS\OFFLINE
    CD %NETX_HOME%
    CALL SET-NETX.CMD
    CD %COBOL_HOME%
    SSBPPC10 JOBOMNI X
    SET JH=C:\J2SDK1.4.2_04\BIN
    SET OMNI_HOME=C:\CS47AUXS\OMNILOADJAR
    CD %OMNI_HOME%
    %JH%\java -jar omniload.jar
    Can anyone shed some light here? Even when I execute the application from the command line the new DOS box doesn't become visible nor can I see any errors. If I could just get that visibility, I could probably figure out what is going wrong.

    Same problem with me as well.... Badly looking for a solution...
    I predict the following:
    - If your batch file has pretty less number of dos/shell commands then it gets executed fine with exec() and proc.waitFor();
    - If you increase the number of dos/shell commands in the bat file then try executing it then it definately hangs at proc.waitFor();
    Even "cmd.exe /C C:\\test.bat" hangs... if the commands are more...
    Is this some sort of bug? or am i doing anything wrong? I tried searching for solution on the net and search forums... but couldnt find a solution for the same.. not sure where i missed, what i missed...
    Incase some one finds a solution.. do post it here...
    Message was edited by:
    amadas

  • Unable to open files from within Photoshop

    Hi, I've updated to CC 2014.2.1 and I'm continuing to get this message when I attempt to open a file from within Photoshop (it started when I installed a beta versionYosemite, so I switched back to Mavericks, but it didn't help, [so many updates with Cloud, I lose track..]).  I noticed someone else with a similar problem, but from 2013. Anyway, I get this message: 'Could not complete your request because of a program error.'  I restart Photoshop, then I can open a few files, but then the errors start over.
    Also, I'm not really familiar with the 'forum' means of support, but if anyone knows a solution to this beyond 'google it', which I've done, I'd appreciate it.
    -Stu

    Thanks Jeffery Tranberry—this did the trick. BUT while an answer to my issue gave the following feedback to Adobe....
    Use of COVERFLOW if EXTREMELY IMPORTANT (in Photoshop) when working fast with hundreds of images. Who the heck labels every single image asset file—and knows what that is without a visual cue? WHEN IS THIS ON THE ROADMAP TO BE FIXED?????????? UGH! MAVERICKS has been coming for MONTHS. Why was this not vetted on Adobe's end as a valuable roadmap item for its users? I would have ranked this a high item from a user experience end and fought for inclusion on the developers roadmap. What is the ETA on a real fix concerning Mavericks & Coverflow use with Photoshop?

  • How to run a batch file from within an Oracle 9i Form?

    Hi, I would like to execute a batch file from within an oracle form - I'm assuming there's some PLSQL command I can use? I am not allowed to use any third party libraries, and I'm hoping I can get away without having to progam any C or Java to do it. I don't want to run my java program from my form, but my lecturer is insisting on it.
    Can anyone help?
    Thanx
    Adam

    Can u give me an example
    I need to run a batch files that does a series of step doing dir ( in 3 different folders)
    after each one it shows on screen with a pause & user press enter
    can this work out ?

  • Using System.err.println() from within the classes of WAS ?

    hi,
    I am using admin.jar,a jar file which is being used by Web Application Server in my own class.
    I am referencing some of the classes from admin.jar from my class.
    I tried to print some trace statements,using System.err.println() from within the classes in admin.jar but they did not reflect in defaulttrace0.trc.
    I made these changes in the admin.jar being used by WAS.
    I restarted the server and even restarted the machine but without success.
    I want to know how to print System.out.println() statements from within the classes in admin.jar.
    Also, am i looking for these statements in the right file for eg. defaulttrace0.trc. or is it some other file that i need to look into.
    I need urgent help on this.
    Reward points assured.
    thanks a lot.
    Saurav

    thanks craig,
    ur answer has helped me a lot.but it didnt quite help me.
    nevertheless i am trying to set different levels of severity.
    Is there anything else that i can do.
    Also,i commented out a line of code today in one of the class DataSourceManagerImpl.java in sapj2eenginedeploy.jar
    for eg. temp.delete in it deploy method.Buth that line still executed.
    I m totally lost as to wht to do.
    I m trying to create a datasource from my application in WAS.For that i m using the WAS APIs.But its not working completely.
    I am using the above jar and its method createdatasource.
    I am callin it from my application's class.
    It creates a datasource and i can see it in JDBC Connector list in Visual Administrator.But it appears with red sign meaning its not started.When i start it from the tool then it starts.
    But in defaulttrace.trc file it shows an error "FileNotFindException". This happens,and i am very much sure, in the deploy() of DataSourceManagerImpl.java class of sapj2eenginedeploy.jar.
    i want to apply println inside this method so i may know where exactly i ma getting the error and get so more info so i may solve my problem.
    pls help me.
    its really urgent.
    thanks again
    saurav

  • Request.getParameter("blah") from within a class?

    Hi Everyone,
    Not sure if I'm missing something...
    I'm trying to create a class that will examine and parse information from a form, and then pass it to another class that knows how to handle the input.
    A request in the form of http://www.domain.com/stuff.jsp?1=param1&2=param2&4=param4
    stuff.jsp only has this:
    UrlRequestGetter params = new UrlRequestGetter();
    out.println(params.getTheParams());and the general idea for the UrlRequestGetter class is this:
    public classUrlRequestGetter {
         public String getTheParams(){
              String request1 = request.getParameter("1");
                    String request2 = request.getParameter("2");
                    String request3 = request.getParameter("3");
                    String request4 = request.getParameter("4");
    }Within that class, there will be various actions to check whether or not fields are empty, etc. The big point is that i'm trying to do a request.getparameter from within a class. When i try to compile, I get:
    cannot find symbol
    symbol:variable request
    Any thoughts?
    Thanks!

    Take a step back from all that, and read the error message: cannot find symbol: symbol:variable request
    Is "request" a variable you have declared or passed into your method getTheParams() ?
    Where is it supposed to get the request from?
    "request" is an implicit variable to your JSP. If you want to use it in your bean, you have to pass it as a parameter.
    I'm trying to create a class that will examine and parse information from a form, and then pass it to another class that knows how to handle the input.You might consider using the <jsp:setProperty> tag to map parameters to bean attributes automagically.
    Or the BeanUtils.populate method provided by Jakarta Commons BeanUtils.
    Or any framework like struts, jsf, spring, stripes....
    Cheers,
    evnafets

  • Reading file from an external folder.

    Iam tring to read an external file from the C:\ drive. Given below is the code.
    DATA LV_XLS(100) TYPE C.
    DATA LV_CONTENT TYPE XSTRING.
    LV_XLS = 'C:\XML\Report.xls'
    READ DATASET LV_XLS INTO LV_CONTENT.
    But when i execute iam getting an error - FILE NOT FOUND. How do i read file from an external folder. My requirement is to strictly read from an external folder. Please help.
    If this is not possible please advice as to how the file could be moved to the SAP root directory. Iam not able to locate the root.

    hi,
    This is the code to read a excel file from the local system.
    Read data set you are using in your code corresponds to files on application server thats the reason its throwing an error.
    * Parameter variable declaration for browsing the file location       *
    PARAMETERS:
      p_file TYPE ibipparms-path OBLIGATORY.
    data:
    it_text TYPE truxs_t_text_data .
    AT SELECTION-SCREEN  ON VALUE-REQUEST FOR p_file.
      CALL FUNCTION 'F4_FILENAME'
       EXPORTING
         program_name        = syst-cprog
    *   DYNPRO_NUMBER       = SYST-DYNNR
         field_name          = ' '
       IMPORTING
         file_name           = p_file.
    START-OF-SELECTION.
      CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
        EXPORTING
    *   I_FIELD_SEPERATOR          =
    *   I_LINE_HEADER              =
          i_tab_raw_data             = it_text
          i_filename                 = p_file
        TABLES
          i_tab_converted_data       = t_cust
      EXCEPTIONS
        conversion_failed          = 1
        OTHERS                     = 2
      IF sy-subrc <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    This is to read a text file from local system.
      CALL FUNCTION 'GUI_UPLOAD'
        EXPORTING
        filename                      =  'D:\FILEINTERFACE\file.TXT'
    *    FILETYPE                      = 'ASC'
         has_field_separator           = 'X'
    *    HEADER_LENGTH                 = 0
    *    READ_BY_LINE                  = 'X'
    *    DAT_MODE                      = ' '
    *    CODEPAGE                      = ' '
    *    IGNORE_CERR                   = ABAP_TRUE
    *    REPLACEMENT                   = '#'
    *  IMPORTING
    *    FILELENGTH                    =
    *    HEADER                        =
        TABLES
          data_tab                      = t_kna1
    *  EXCEPTIONS
    *    FILE_OPEN_ERROR               = 1
    *    FILE_READ_ERROR               = 2
    *    NO_BATCH                      = 3
    *    GUI_REFUSE_FILETRANSFER       = 4
    *    INVALID_TYPE                  = 5
    *    NO_AUTHORITY                  = 6
    *    UNKNOWN_ERROR                 = 7
    *    BAD_DATA_FORMAT               = 8
    *    HEADER_NOT_ALLOWED            = 9
    *    SEPARATOR_NOT_ALLOWED         = 10
    *    HEADER_TOO_LONG               = 11
    *    UNKNOWN_DP_ERROR              = 12
    *    ACCESS_DENIED                 = 13
    *    DP_OUT_OF_MEMORY              = 14
    *    DISK_FULL                     = 15
    *    DP_TIMEOUT                    = 16
    *    OTHERS                        = 17
      IF sy-subrc <> 0.
    * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    *         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    PS.  In both of the above mentioned function modules make sure that the file structure is similar to the internal tables used.
    Thanks
    Sharath

  • Reading file from a folder : File dir = new File(URL+aFile_to_add);

    Hi,
    Trying to read files from a dir on my site using a JSP and bean. I've tried nearly every option for the filepath I can think of and it doesn't seem to be finding the file...
    Here's the bit:
    String URL = "images\\wedding_photos\\";
    File dir = new File(URL+aFile_to_add);
    String[] files = dir.list();
    It's working locally, but I obviously had to change the path from c://... to the above.
    The full code is listed below in the addWedding Method
    The method removes the .jpg from the file name and then writes the new file name to a database
    Thanks for any insight.
    Regards
    Jim
    public String addWedding(String aFile_to_add)
    String feedback = "unset in scrubWedding";
    try
    String URL = "images\\wedding_photos\\";
    Class.forName("org.gjt.mm.mysql.Driver");
    java.sql.Connection connection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/rhwedd2_shop?user=**********&password=*******");
    java.sql.Statement statement = connection.createStatement();
    File dir = new File(URL+aFile_to_add);
    String[] files = dir.list();
    if (files == null)
    feedback = "Sorry, couldn't find the file "+aFile_to_add+"...no files have been added.";
    else
    for (int i=0; i<files.length; i++)
    // Get filename of file or directory
    String filename = files;
    String no_extension;
    //convert string array element into a char array
    char [] charsfilename = filename.toCharArray();
    int newlength = charsfilename.length -4;
    no_extension = String.valueOf(charsfilename,0,newlength);
    filesAdded = filesAdded+", "+no_extension;
    // Show the new file names in stack trace without extension
    //System.out.println("String no_extension " + no_extension );
    statement.executeUpdate("INSERT INTO items VALUES ('"+no_extension+"',5,'"+aFile_to_add+"')");
    // write to table with item_id and weddingid
    feedback = "The following files have been added..."+filesAdded;
    if (statement != null )
    statement.close();
    if ( connection != null )
    connection.close();
    catch(Exception e)
    feedback = "Into the catch";
    e.printStackTrace(System.err) ;
    return feedback;
    }//end addWedding

    String URL = "images\\wedding_photos\\";
    File dir = new File(URL+aFile_to_add); ok, ok, I'll answer...
    1) What gives you the impression that the relative directory "images\\..." exists (or, in other words, what makes you think your current directory is the parent of "images"?

  • Open Data Set Error while trying to read file from non SAP server

    Hi all,
    is it possible to read data from non-SAP application Sever?
    I'm using OPEN DATASET p_filin FOR INPUT IN LEGACY TEXT MODE CODE PAGE '1504',
    Where p_filin is other Windows server.Our applicition server is under Unix.Is it a problem?
    I make test to read file from SAP application server and it was ok.So how to call other server?
    Thanks!

    Hi,
    Yes it is possible to read data from a non SAP server through the statement OPEN DATASET.
    The important thing to check is that the SAP Server got enough access to the non SAP server so it can perform a reading/writing process depending on your needs.
    You should contact your network administrator and BASIS to help you check the permissions. This can be pretty tricky, specially if the servers are in different domains.
    Regards,
    Gilberto Li

  • Unable to open CS3 Files from within the application

    Good Afternoon
    I work as IT Support for company looking after 10 ADOBE Creative Suite 3 end-users.
    One of my colleagues is unable to open Illustrator or Photoshop files from within their respective applications, but she is able to open them from outside ok.
    I have reinstalled ADOBE CS3 and this appeared to resolve the issue, only for it to return again a day later.
    She has no other applications or processes running during her attempt to open a file from within CS3.
    She will click on Open and the 'Open File' window will flash up for a split second, then the whole application will be frozen. It is as if the 'Open File' window is there, but isnt there because we cant click on it, but the application thinks it is displaying and so she can not click on anything else. The only way to close the application is to end the process under Task Manager.
    While I am sitting here tearing what little amount of hair I have on my head, can anyone point me in the right direction?
    Is this a Windows problem or an ADOBE problem do you think?
    I look forward to any replies.
    Thank you

    Hi Bob
    Ok...
    Opened illustrator but was unable to open the 'Open File' window.
    Changed the screen resolution, it suddenly worked!
    Changed the screen resolution back to as it was before, it also worked.
    What could cause this do you think?
    By simply changing the screen resolution and back again, the 'Open File' window displayed itself.
    Very bizarre!

  • How to Print  a PDF file from within a ABAP ( from gui input)

    Hi champions,
    This is Client requirement:
    "We need to print a PDF file from within a ABAP"
    We first need to test whether it is even feasible, and then if it 
    is, then write a small test ABAP which can accept a PDF file
    name as input (from GUI) and then print it to a SAP print
    device (printer).”
    Please Can any body help me on this.......
    It's urgent
    Thanks
    Basu

    Check following blog for PDF output from SAP
    /people/thomas.jung3/blog/2005/04/28/setting-up-an-adobe-writer-for-abap-output
    anya

  • Timecapsule - opening files from within an application!!

    I have a time capsule... 500Mb
    I have an airport extreme with external hard drive - 250 Mb.
    Through Finder, I can place files on either of these drives and open them from Finder.
    If I open Numbers, or Pages, then try to open any file on Timecapsule, it appears that there are no files
    If I open Number, or Pages, then try to open any file on the Airport Extreme external disk - it works!
    Simple question really.... why cannot I see or open any file from within an application that resides on Timecapsule?? It's driving me crazy!!!
    Message was edited by: GrahamDurkin

    File dialogs have the ability to "group" items, which prohibits you from changing sort options.  The group icon looks like six little squares.  Click on it and instead of grouping by date, group by "none" and then you can change sort orders.

  • How to open Adobe Reader file from another native IOS application?

    There is an existing thread, but I want to re-open it because I think this is an important feature that we need badly.  I was wondering if there is any plan to add this feature so we can open PDFs directly into Adobe from the web / other apps.
    How to open Adobe Reader file from another native IOS application?
    Basically, we just want to use a custom URL scheme to open a specific document in the App.  Currently, this only opens the app but does not load the file.
    APB

    Not to hijack the conversation but I can explain why this would be useful for both the above case and another.
    What I believe Pavel is talking about is setting up a "URL Scheme" for the Adobe Acrobat iOS application so that you can easily open a PDF specifically in Adobe Acrobat iOS from other native applications and even from web applications opened within Safari. This is particularly useful if your application requires some of the specific features in Adobe Reader iOS to grant them the best experience possible and you want to encourage this.
    Another case: If you're using Adobe Livecycle's document security modules (that encrypts PDF files so that Adobe Acrobat must "phone home" to decrypt and view the document), these PDF's can only be viewed inside the Adobe Acrobat application and appear as blank in most other PDF readers. Having a URL scheme allows your application utilising this functionality to have a 1 click step to view the PDF rather than the current non-user friendly process:
    - Within Safari, touch the PDF link (appears as blank in the default Safari PDF reader, which in itself is confusing)
      - Touch "Open in..."
      - Touch "Adobe Acrobat"
    We have an immediate need for this functionality for the example above. I can resubmit it in a separate post if necessary.

  • Reading files from phone memory

    Hi All,
    I am developing a Universal application using vs 2013 in windows phone 8.1. I am stuck in finding and reading file from phone memory.
    I have used the Localfolder syntax
    StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;but it didn't worked. Its Showing the application data. But i want to read the file exixts in my phone memory.Then i have usedStorageFolder file = KnownFolders.DocumentsLibrary;but as i read from the forums its not suggested way to approach. Please suggest me the way to find the file from phone memory and to read it.Any help would be appreciatable!!

    Hi sarika,
    That is expected, see the documentation for more information:
    App capability declarations at Documents section, you need declared at least a file type association.
    Furthermore You can't use the Documents library in a Windows Phone Store app.
    You can't publish a Windows Phone Store app that specifies the documentsLibrary capability to the Windows Phone Store. The Store blocks the publishing of the app.
    Your app can't access the Documents library in the phone's internal storage. If another app creates a Documents folder on the optional SD card, however, your app can see that folder.
    --James
    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.

  • Reading files from Java embedding.

    Dear All,
    My BPEL process has a Java embedding node, and it invokes a Java class, in which an xsl file is being read for transformation.
    I deploy the xsl files directory together with BPEL package (the jar). In the package, I put the file dir at the root level, which means the jar structure looks like the following:
    /BPEL-INF
    /META-INF
    /xsd
    /xsl
    *.wsdl
    *.bpel
    bpel.xml
    The java class is package in /BPEL-INF dir, it's supposed to read files from /xsl dir.
    My question is, how should I put the file path?
    I tried "/xsl/***.xsl" and "../xsl/***.xsl", neither works. Although "/xsl/***.xsl" worked when testing in JDeveloper, but FileNotFound after deployment.
    Thanks,
    Haibo

    I solved this issue by putting the files dir in /BPEL-INF/classes/. and use ClassLoader to read in those files.

Maybe you are looking for

  • BUG in WLE IDL compiler

    Hi, I want to report a bug in the WLE5.0 IDL compiler. It occurs in the generated stub, skeleton and implementation files (*c,*s and _i) files. Here is a description of the problem: Say we have the following IDL: Filename:filewithmoduleM1.idl module

  • Decode 2D barcode using Java 6

    Hi, I need help with barcode decompression. Im using "Paper Forms Barcode" (QR Code type) and using option "Compress data before encoding". I need decompress data by Java program so im using default Java Inflater class. I have read many tutorials and

  • Can we determine if Acrobat is running inside a browser?

    I'm looking for a javascript property or method to determine if Acrobat is running standalone or in the browser, as I'd like to use that to made certain of my custom toolbar buttons available or not. Is there anything available? Thanks -

  • Can I store photos and music in I cloud and relieve space on my hard drive?

    can I store photos and music in I cloud and relieve space on my hard drive?

  • Creating a border in a poster

    I want to create a border for a poster I'm making. I put a white rectangle on the poster. I tried to put another rectangle, smaller, in the center to create a transparent area so that just the "border" remained. I can't seem to get the inner rectangl