views:

520

answers:

4

I am working on a project in netbeans that requires the running project to have root privileges.

I would like it so that each time I push "Run Project" (F6) my project is run as root, so with the equivalent of "gksudo javac Main", as it is has a GUI.

One option is to start netbeans with root privileges, easily done by editing the shortcut to "gksudo netbeans". But as I have multiple projects in netbeans this has meant that for each project I must run netbeans with root privileges, this is not what I want.

Another option, of course, is to simply run my project from the shell, but this is not ideal either.

I think this is possible by altering this projects build.xml file but am having trouble figuring out how.

After some research it would be seem that this is rather an Apache Ant question, I have access to build.xml which I can modify so I need to know how to get Ant to run my project with sudo/gksudo. However, I can't figure out how to do this or if it is possible.

A: 

Why gksudo instead of just 'sudo java Main'? In either case, you can just set your sudoers file (in /etc) to not require a password from your user and you should be good to go.

Shane C. Mason
Pretty sure if he used gksudo he wouldn't have to set up sudo to work without a password.
MitMaro
gksudo simply because when I push F6 I would prefer a prompt to pop up asking me for my password rather than having to type it in my output box.sudo or gksudo, either is good, the solution to one will be the same solution to the other!Editing sudoers would mean I could easily use sudo instead of gksudo but it does not solve my actual problem, I still need a way to run the class with sudo in the first place.
Andrew
A: 

run Netbeans by user super

tommaso
...which is exactly what the querent mentioned, and explained why he couldn't use it.
Andrzej Doyle
A: 

A very dumb tip: create new Ruby application, put it near to your java project, and put the following to main.rb

PROJECT_DIR = "MyJavaApp"
# These must be relative to java project's dir or absolute paths
CLASSPATH = %w[dist/lib/*.jar /usr/share/some/other/lib.jar]
SUDO_BIN = "gksudo"
# Append your app's jar file into cp!
MAIN_CLASS = "com.andrew.myapp.Main"
# Put your own opts here (-Xmx, -Xms, etc)
JVM_OPTS = "-client" 

###############################################
# Calculating project's root. 1st dir is a root of Ruby project, second is a common root
projroot = File.expand_path(File.dirname(__FILE__) + "/../.." 
java_args = JVM_OPTS
cp = []
# It uses globbing to expand *.jar style stuffs
CLASSPATH.each do |path|
  cp += path.start_with?("/") ? Dir[path] : Dir[File.join(projroot, PROJECT_DIR, path)]
end
java_args += "-classpath " + cp.join(File::PATH_SEPARATOR) + " "
# The heart of our work...
system(SUDO_BIN + " java " + java_args + " " + MAIN_CLASS)

Set the ruby project to main project.

I didn't tried it, but there must be only typos.

I know, this is not a best solution, but NB starts java projects internally and seems doesn't provides a configuration option to do it.

Btw: this solution needs installing Ruby on Rails plugin into NetBeans. The script doesn't exits while your java program is running.

Important: this script assumes it runs under Unix-compatible OS (where an absolute path begins with '/')!

Gabor Garami
+1  A: 

Assuming this is a "Java SE" project (as opposed to, say, a web app or a Ruby program). First, turn off Compile on Save under Compiling in project properties.

Second, add to build.xml:

<target name="-init-macrodef-java">
    <macrodef name="java" uri="http://www.netbeans.org/ns/j2se-project/1"&gt;
        <attribute default="${main.class}" name="classname"/>
        <attribute default="${run.classpath}" name="classpath"/>
        <element name="customize" optional="true"/>
        <sequential>
            <exec executable="gksudo" failonerror="true">
                <arg value="--"/>
                <arg value="java"/>
                <arg value="-classpath"/>
                <arg path="@{classpath}"/>
                <arg line="${run.jvmargs}"/>
                <arg value="@{classname}"/>
            </exec>
        </sequential>
    </macrodef>
</target>

There are other things you can fine-tune but this should be enough to get you started.

Jesse Glick
Thankyou, that is exactly what I wanted! Perfect
Andrew