Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: edit .conf file in /etc linux (java SE)

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default edit .conf file in /etc linux (java SE)

    Hi everyone!
    This time I have a java SE web start application hosted in a Linux/Debian server
    I need to edit a .conf file from the /etc directory folder in the Debian server but I have no idea how to access this file if it's not within the public /www folder. I think it has to do with apache users and permissions, but my linux knowledges are just few

    Thanks in advanced!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: edit .conf file in /etc linux (java SE)

    You need sudo permissions to edit files in that directory. What are you trying to do by editing this file? If you need to edit files that require root/sudo access, in my opinion you should probably rethink the design.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    I am trying to modify a .conf file in the /etc/asterisk (a voice ip PBX) that gives permissions to callers. I dont know much about debian or linux, it would be easy to modify a file in the www folder, but how can I access this file instead?
    I need to remove or add some lines to the .conf file eventually.

    thanks 4 ur answer
    Last edited by luisp88; June 25th, 2012 at 01:58 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: edit .conf file in /etc linux (java SE)

    I am not familiar with that file or library so I cannot guide towards alternatives. Unfortunately I can only encourage you to search for alternatives - permissions are setup on linux for a reason and whatever is attempting to modify the file most likely needs sudo access, but in doing could open up a lot of security holes.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    I only need to modify one .conf file from /etc/asterisk , I have no problem giving rights to edit it. In fact, I made Apache to show me the file to the public and I can retrieve it from the server (read it), but when I need to save the changes (or replace the file at the same directory) I am having problems because I originally have a URL text, then parse it to String in the application, and then need to upload or rewrite it to the URL. That last step is giving me an headache!

    Thanks in advanced for your help copeg.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    I have tried some codes, but none of them work

    Just as simple as trying to create and write a .txt in the server

    // Open an output stream
    URI uri = new URI("http://172.16.64.54/test/myfile.txt");
    File file= new File(uri);
    fout = new FileOutputStream(file);

    // Print a line of text
    new PrintStream(fout).println("hello world!");

    // Close our output stream
    fout.close();

    throws Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI scheme is not "file"

    Obviously I dont know how to manage Files and URLs when writting. The folder test is created with all permissions (777)
    I really need to write or copy a file to the server. Any ideas?

  7. #7
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    I'm not sure what the problem is here. It looks like you have the permission problem taken care of (though a non-executable file in Unix should be 666, not 777) but now you're trying to update a file via HTTP? You can't do that. Not even close. You will need something on the server side to do the update. In Java you'd need a Tomcat/Glassfish/<your favorite app server> to do that. If you are just running Apache you may need something like PHP or even CGI.

    Can you describe a bit more about the over all architecture of the system?
    Need Java help? Check out the HotJoe Java Help forums!

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    There is a server (Debian) that host the .jar files (in the public /www directory of Apache). I execute them thru java web start (it downloads the resources to the local machine) on Windows. I need to modify or replace a file located in /etc/asterisk folder within the debian server. How can I upload or copy a file to the debian directories from a java .jar running in a windows client?

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: edit .conf file in /etc linux (java SE)

    As stdunbar mentioned, you need something server side to do this. If you are only running apache, then you need some script to be executed. You can do so by creating a script that your apache configuration is capable of running (perl, php, etc...) and is accessible via a URL. The script accepts the info to change, and writes it to the file. The client (webstart) app then accesses this script through its respective URL

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    ok I understand, maybe I can make a php or perl script to do this. How do I give the information to the script? Is there any class in java to communicate with a server side script? thanks for your help!

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: edit .conf file in /etc linux (java SE)

    Quote Originally Posted by luisp88 View Post
    ok I understand, maybe I can make a php or perl script to do this. How do I give the information to the script? Is there any class in java to communicate with a server side script? thanks for your help!
    If you do not have some type of RMI or java server set up so that you remotely communicate with the server (apache does not by default as far as I am aware), then you can pass the information as POST using a URL
    Lesson: Working with URLs (The Java™ Tutorials > Custom Networking)
    Server side, the script retrieves the post information, validates the information, and uses it as needed.

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    Thanks for the link, I am already trying to figure it out.
    Its a good idea to use URL passing to edit a whole .conf file? I mean its a long string, there is no problem sending it to a php script via url?
    I dont know about servlets, but I think they dont work in Apache, its better to set up a server that executes them ? (I think tomcat and glassfish are the ones)

    thanks a lot copeg

  13. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    Ok I made a php script to edit the .conf file and it works fine giving apache the permissions. But the problem is sending the information from JAVA to PHP script.
    JAVA:
    URL url = new URL("http://172.16.64.54/prueba/url.php");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write("test);
    wr.flush();

    and the PHP script is supposed to be executed, but it doesnt. When I type the php script in the browser, it works (forget about the passing arguments) but when I execute my java program, it doesnt execute!
    Is there another class to work with http urls?

  14. #14
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    You may need to read the response for the request to be sent. I'm fairly sure it's a 'lazy' process. Read the API docs for HttpUrlConnection - I think reading a response header or the response body will complete the exchange.

  15. #15
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: edit .conf file in /etc linux (java SE)

    I made it work, thanks a lot. I will check HttpUrlConnection for more information.

Similar Threads

  1. how to edit hosts file in windows7 using java program ?
    By vijay_p in forum Java Theory & Questions
    Replies: 3
    Last Post: December 2nd, 2011, 06:14 AM
  2. jar file works on XP but not on Linux
    By cl2606 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 10th, 2011, 09:19 AM
  3. Replies: 1
    Last Post: June 7th, 2011, 11:53 AM
  4. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM
  5. Replies: 2
    Last Post: August 1st, 2010, 06:29 AM

Tags for this Thread