non-static method xxx cannot be referenced from a static context
hi.. I have some code and I get this error:
set_ID.java:147: non-static method setGroupByGid(int) cannot be referenced from a static context
XenonGroup.setGroupByGid(gid);
the setGroupByGid(int) function is part of the com.xenon.unix package, the setUid(int) is in the javaunix package. the setUid(int) works fine... but I can't get the other function to work. The setUid function is declred as:
publicstaticvoid setUid(int uid )
throws PermissionDeniedException
theXenonGroup.setGroupByGid(gid) is declared as
void setGroupByGid(int uid)
- native method to set the Java group object using a group ID
import java.io.*;
import java.util.*;
import javaunix.*;
import com.xenon.unix.*;
class StreamGobblerextends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
publicvoid run()
{
try
{
InputStreamReader isr =new InputStreamReader(is);
BufferedReader br =new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) !=null)
System.out.println(type +">" + line);
}catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
publicclass set_ID
{
publicvoid main(String args[])
{
try
{
String cmd ="whoami";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
System.out.println("The Starting Gid: "+UnixSystem.getGid()+"."
WaitAMoment(1)
\\gettting error here ~~~~~~~~calling method at end ~~~~~~~~~~~~~~~~~~~~~~~~~
setGid(1000);
System.out.println("The new Gid: "+UnixSystem.getGid()+".");
}catch (Throwable t)
{
t.printStackTrace();
}
}
protectedstaticvoid WaitAMoment(int sec)
{
try
{
Thread.sleep(sec*1000);//
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
\\gettting error here ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
publicvoid setGid(int gid)throws PermissionDeniedException
{
XenonGroup.setGroupByGid(gid);
}
}
[4354 byte] By [
geema] at [2007-10-3 1:01:08]

ok I have changed my code... and it compiles now, but it doesnt do what I want it to do yet.
public class set_ID
{
public static void main(String args[])
{
try
{
String cmd = "whoami";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
System.out.println("The Starting Uid: "+UnixSystem.getUid()+ ".");
WaitAMoment(1);
System.out.println("The Starting Gid: "+UnixSystem.getGid()+ ".");
WaitAMoment(1);
System.out.println("Setting Gid to 1000.");
WaitAMoment(2);
XenonGroup a = new XenonGroup();
a.setGroupByGid(1000);
System.out.println("Gid is now: "+UnixSystem.getGid()+".");
WaitAMoment(2);
System.out.println("Setting Uid to 1000.");
WaitAMoment(2);
UnixSystem.setUid(1000);
System.out.println("Uid is now: "+UnixSystem.getUid()+".");
WaitAMoment(2);
System.out.println("Setting Uid to 0. This should fail!! ");
WaitAMoment(2);
if(UnixSystem.getUid() == 0) //root
{
UnixSystem.setUid(0);
}
else
{
System.out.println("Permission Denied: can't setuid(0) : You must be root to perform that operation");
}
WaitAMoment(2);
System.out.println("Uid is now: "+UnixSystem.getUid()+".");
WaitAMoment(2);
//any error message?
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR");
//any output?
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
// any error?
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
protected static void WaitAMoment(int sec)
{
try
{
Thread.sleep(sec*1000); //
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
and my output looks like:
The Starting Uid: 0.
The Starting Gid: 0.
Setting Gid to 1000.
Gid is now: 0.
Setting Uid to 1000.
Uid is now: 1000.
Setting Uid to 0. This should fail!!
Permission Denied: can't setuid(0) : You must be root to perform that operation
Uid is now: 1000.
ExitValue: 0
the Gid is not geting set to what I want. I am thinking it has to do something with that function being the only non-static, and having to create a new object. That's how the methods are defined in those APIs... so i dont know if i need to modify the API and create a static class setGid or what to do... any ideas?
geema at 2007-7-14 17:57:09 >
