Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Using java configuration file to call a python script

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear JavaRanch memebers,

Here is my sample code:



As it can be seen in the code, this part:



contains the path of the python script that will be executed. But this source code can only be used in my computer and it's more specific.

Is there a way to make it more generic? so that it can be used in other computers as well? I have heard about something called configuration files, but being very new to java, I was not able to understand it.
And i also tried creating a file called path.properties and copying it in the src folder of netbeans project. The content of path.properties is

Path=C:\\Users\\Balkishore\\Documents\\NetBeansProjects\\Testinstrument_Rest\\build\\web

But i don't have any idea how to call this property file in my code, and where to call this all i know is to call it i have to use:


But seriously i have no clue where to add this piece of code in my source code.

Any help would be very much appreciated.

Thanks a ton in advance.
 
Ranch Hand
Posts: 92
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balkishore,

so from what I understand is that you would prefer the path in this segment of code to be more generic, or platform-independent (i.e. doesn't matter whose computer it runs on).



You have multiple options, two of which you have suggested.

  • Use properties files
  • Use system properties


  • My own preference, since this is a plain Java application, is to use system properties. This means that an Environment Variable, called PYTHON_PATH (or something similar) is set. The value of this variable is the path to Python on the users computer (in your case C:\\Users\\Balkishore\\Documents\\NetBeansProjects\\Testinstrument_Rest\\build\\web).

    In your code, to get this path, you only require;



    If you would prefer to use a configuration file, again you're almost on the right path, but spend a few hours reading the tutorial on Resource Bundles to better understand the approach (I myself am starting to forget it)

    I hope this helps.

    John
     
    Balkishore pandey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi John McParland,
    Thank you very much for replying.
    But when I run this script, i get an HTTP 500 error.
    Edited: I use to get this kinds of error before, when my python file is not read.
    Any help would be very much appreciated.

    Regards.
    Balkishore
     
    John McParland
    Ranch Hand
    Posts: 92
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Balkishore pandey wrote:Hi John McParland,
    Thank you very much for replying.
    But when I run this script, i get an HTTP 500 error.
    Edited: I use to get this kinds of error before, when my python file is not read.
    Any help would be very much appreciated.

    Regards.
    Balkishore



    Have you tried debugging the process? What about printing out the value obtained from System.getProperty(PYTHON_PATH)?

    Where are you getting the 500 error exactly?
     
    Balkishore pandey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi John McParland,
    I am getting this error when i test my restful wen service in the browser.
    It is an request failed error,which means the python file is not read in the program.
    But i didn't print the value of System.getProperty(PYTHON_PATH)
    I will print it and will inform will let you know.

    Thanks you very much.
    Regards.
     
    Balkishore pandey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi John McParland,
    I have a small doubt,
    How should i print System.getProperty(PYTHON_PATH)?
    I used System.out.println(System.getProperty(PYTHON_PATH)); and stopped debugging a line after this.
    But not getting any thing.

    What i am getting is
     
    Balkishore pandey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi John McParland,

    Are you sure i have to use java.io.file in this line



    As java.io.File is used to read the path or file name and it is not used to read the value of the string.
    Do apologize my stupid-ness if the question was wrong.
     
    John McParland
    Ranch Hand
    Posts: 92
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Balkishore pandey wrote:Hi John McParland,
    I have a small doubt,
    How should i print System.getProperty(PYTHON_PATH)?
    I used System.out.println(System.getProperty(PYTHON_PATH)); and stopped debugging a line after this.
    But not getting any thing.

    What i am getting is



    This appears to be the output of building the project rather than debugging it. I was thinking about using, for example, the Eclipse debugger with a breakpoint set at the place you are calling exec.

    You can print the system property like this;



    Of course I don't know exactly where System.out is going in your application. Is it a web application? Does it using logback or another logging mechanism? If so it would be better to log the value of PYTHON_PATH to there instead.


    Are you sure i have to use java.io.file in this line



    Yes I'm sure it is java.io.File. After all, the value in pythonPath should now be the location of python.


     
    Balkishore pandey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi John McParland,
    I tried debugging, i use netbeans by the way, by placing the end point. But still i dont get any thing. And yes it is a web application. I don't use and kind of logging mechanism.
    Is it possible to use properties or configuration files? If so how can I use them?
    Any help would be very much appreciated.

    And Thank a ton for replying.
    Regards.
     
    John McParland
    Ranch Hand
    Posts: 92
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Balkishore pandey wrote:Hi John McParland,
    I tried debugging, i use netbeans by the way, by placing the end point. But still i dont get any thing. And yes it is a web application. I don't use and kind of logging mechanism.
    Is it possible to use properties or configuration files? If so how can I use them?
    Any help would be very much appreciated.

    And Thank a ton for replying.
    Regards.



    Well if you've put a break point on the code, at the point of calling exec, and it doesn't reach it then you've got far bigger problems. Work backwards from that point, placing break points earlier in the code until you can figure out why it is not executing the "exec" part.

    Using properties or configuration files is rather moot until you can get it working as is. Besides, these mean that for every new machine you deploy to, you'll need a new properties file. I'm not sure this is exactly what you are looking for.

     
    Balkishore pandey
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi John McParland,
    When i directly specify the path in java.io.file, the source code works perfectly.
    And by the way, it is fine if i have t write a new property file for a new machine.
    Any help would be very much appreciated.

    Regards.
     
    John McParland
    Ranch Hand
    Posts: 92
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Balkishore pandey wrote:Hi John McParland,
    When i directly specify the path in java.io.file, the source code works perfectly.
    And by the way, it is fine if i have t write a new property file for a new machine.
    Any help would be very much appreciated.

    Regards.



    As I said earlier, you'd be better to read the tutorial on Resource Bundles to find out how to do this.

     
    incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
    free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
    reply
      Bookmark Topic Watch Topic
    • New Topic