java - Error: Could not find or load main class com.example.Test -
i searching how access private field of class other class , found can done using reflect.field in following code, when try execute code error saying :error: not find or load main class com.example.test
why happening?
test.java
package com.example; import java.lang.reflect.*; class test { public static void main(string[] args) // ease of throwaway test. don't // normally! throws exception { other t = new other(); t.setstr("hi"); field field = other.class.getdeclaredfield("str"); field.setaccessible(true); object value = field.get(t); system.out.println(value); } }
other.java
package com.example; class other { private string str; public void setstr(string value) { str = value; } }
compile both classes using javac
javac other.java javac test.java
and run java test
while in eclipse need select test class using run configuration option.
Comments
Post a Comment