The code below compiled successfully here https://www.compilejava.net/ but execution fails
Error: Could not find or load main class ClassDemo
whereas it does have a main entry point. Why ?
package com.tutorialspoint;
import java.lang.reflect.*;
public class ClassDemo {
public static void main(String[] args) {
try {
ClassDemo c = new ClassDemo();
Class cls = c.getClass();
// returns the array of Field objects
Field[] fields = cls.getDeclaredFields();
for(int i = 0; i < fields.length; i++) {
System.out.println("Field = " + fields[i].toString());
}
}
catch(Exception e) {
System.out.println(e.toString());
}
}
public ClassDemo() {
// no argument constructor
}
public ClassDemo(long l, int i) {
this.l = l;
this.i = i;
}
long l = 77688;
int i = 3;
}
You need to remove the package identifier from your code (first line) since you are using an online compiler/executor.
Hope this helps.
It's because you have a package statement.
Remove that and it will work just fine.
Related
package basicprograms;
public class Progrm {
public long[] ph;
public Progrm(long[] ph){
this.ph=ph;
}
}
The main method:
package basicprograms;
import java.util.ArrayList;
public class UseProgrm {
public static void main(String[] args) {
ArrayList<Progrm> ar = new ArrayList<>();
Progrm p1 = new Progrm(new long[] { 942758427l, 4298578432l, 3425962l });
Progrm p2 = new Progrm(new long[] { 942758427l, 4298578432l, 3425962l });
Progrm p3 = new Progrm(new long[] { 942758427l, 4298578432l, 3425962l });
ar.add(p1);
ar.add(p2);
ar.add(p3);
for (int i = 0; i < ar.size(); i++) {
System.out.println(ar.get(i));
}
}
}
By default, all classes in Java inherit from the Object class. In this case what you are actually printing is Progrm::toString method that is inherited for the Object class and by default is returning the hash. If you would like to print the content of the array(public member ph of the Progrm class) then you should override the toString of Progrm as follows:
public class Progrm {
public long[] ph;
public Progrm(long[] ph) {
this.ph=ph;
}
#Override
public String toString() {
return "Progrm{" +
"ph=" + Arrays.toString(ph) +
'}';
}
}
and the output will be:
Progrm{ph=[942758427, 4298578432, 3425962]}
Progrm{ph=[942758427, 4298578432, 3425962]}
Progrm{ph=[942758427, 4298578432, 3425962]}
For more info on Object::toString, you can refer to :
Why does the default Object.toString() include the hashcode?
You have to override the toString() method in your Program class
now, the System.out.println statements are calling the default implementation of the Object class.
Add this to your Program class:
public String toString() {
StringBuilder b = new StringBuilder("");
for ( long p : ph) {
b.append("Value: " + p + ", ");
}
return b.toString();
}
Afterwards, you can modify it to fit your needs.
Try this:
for (int i = 0; i < ar.size(); i++) {
for(int j = 0; j < ar.get(i).ph.length; j++)
System.out.println(ar.get(i).ph[j]);
}
I am trying to understand how reflection works, i cant able to figure out the problem. In the second for loop method.length is not fetching method from another program.
Following is the code:
public class DriverScript
{
public static ActionKeywords actionKeywords;
public static String sActionKeyword;
public static Method method[];
public DriverScript() throws NoSuchMethodException, SecurityException
{
actionKeywords = new ActionKeywords();
method = actionKeywords.getClass().getMethods();
}
public static void main(String[] args) throws Exception
{
System.out.println(method);
String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx";
ExcelUtils.setExcelFile(sPath, "Test Steps");
for (int iRow = 1; iRow <= 9; iRow++)
{
sActionKeyword = ExcelUtils.getCellData(iRow, 3);
execute_Actions();
}
}
private static void execute_Actions() throws Exception
{
for (int i = 0; i < 11; i++)
{
if (method[i].getName().equals(sActionKeyword))
{
method[i].invoke(actionKeywords);
break;
}
}
}
}
public class Test2 {
//Reflection in Keyword driven Framework
/**
* #authors Venkadesh,Selvakumar work name : Test
**/
static Class Class1 = ActionKeywords.class;
static ActionKeywords Keywords = new ActionKeywords();
public static void main(String[] args) throws Exception {
String sPath = "C:\\Users\\mag6\\Desktop\\toolsqa.xlsx";
ExcelUtils.setExcelFile(sPath, "Test Steps");
Method[] methods = Class1.getDeclaredMethods();
for (int i = 1; i <= 9; i++) {
// This to get the value of column Action Keyword from the excel
String sActionKeyword = ExcelUtils.getCellData(i, 3);
for (int j = 1; j <= 9; j++) {
if (methods[j].getName().equals(sActionKeyword)) {
try {
methods[j].invoke(Keywords);
} catch (Exception e) {
}
System.out.println(methods[j]);
}
}
}
}
}
try with this it works
You are not calling the constructor for DriverScript so the fields are not initialized.
Try changing your methods from static to non-static and fix from there.
Replace
public static Method method[];
with
public static Method method[]= ActionKeywords.class.getDeclaredMethods();
I am new to concepts of java. while preparing my first program of classes with objects i encountered a problem. here is the code and the error..please resolve..
PROGRAM:
class Fact
{
private int i;
private int n;
private int fact;
public Fact()
{ fact=1;
i=1;
}
public Fact( int x)
{ n=x; }
public void getAnswer()
{
while(i<=n)
{fact=fact*i;
i++;}
System.out.println(fact);
}
}
class FactMain
{
public static void main(String dt[])
{
Fact obj= new Fact(6);
obj.getAnswer();
}
}
ERROR:
Main method not found in class Fact, please define the main method as:
public static void main(String[] args)
just change your Parameterized constructor to this
public Fact(int x) {
fact = 1;
i = 1;
n = x;
}
because you declare factorial in default constructor and you are not calling it. So, 0 is assigned to factorial and then you r trying to multiply it. Which makes no sense.
Rename the class file name Fact.java to FactMain.java.
private int fact;
public Fact()
{ fact=1;
i=1;
}
public Fact( int x)
{ n=x; }
Note, your default constructor set fact but constructor Fact( int x) set n. Hence fact is 0. So your output is 0 too.
Solution:
public Fact(int x) {
fact = 1;
i = 1;
n = x;
}
Or,
public Fact(int x) {
this(); // default constructor
n = x;
}
Here is the complete solution:
Create a single class file named FactMain.java, then paste the following code:
class Fact {
private int i;
private int n;
private int fact;
public Fact() {
fact = 1;
i = 1;
}
public Fact(int x) {
this();
n = x;
}
public void getAnswer() {
while (i <= n) {
fact = fact * i;
i++;
}
System.out.println(fact);
}
}
class FactMain {
public static void main(String[] dt) {
Fact obj = new Fact(6);
obj.getAnswer();
}
}
Your main method is in FactMain.java, but you are saving a file as Fact.java.
You will need to save the file as FactMain.java as JVM expects main to be in the same class as the name of .java file.
You have saved your file as Fact.java. So java is trying to find the main class in Fact. Save your file as FactMain.java It should work.
You have defined your main class in FactMain and most probably after compilation while running you're trying to execute
java Fact
And hence you got the error because there is no main method in Fact class.
Once you compile the .java file you will get two class files Fact.class and FactMain.class so you should execute
java FactMain
Move the FactMain class to FactMain.java
FactMain.java
public class FactMain
{
public static void main(String dt[])
{
Fact obj= new Fact(6);
obj.getAnswer();
}
}
Allow the Fact class to remain in the Fact.java file
Fact.java
public class Fact {
private int i;
private int n;
private int fact;
public Fact() {
fact = 1;
i = 1;
}
public Fact(int x) {
this();
n = x;
}
public void getAnswer() {
while (i <= n) {
fact = fact * i;
i++;
}
System.out.println(fact);
}
}
Compile the classes...
javac {package path}\FactMain.java
Run the main class
java {package path}.FactMain
Our professor assigns us exercises through the jarpeb sytsem (Java Randomised and Personalised Exercise Builder). So the variables names are random.
public class Eczema extends Thread {
private int aurite;
private int[] serlvulate;
public Eczema(int[] serlvulate) {
this.serlvulate = serlvulate;
}
public int getAurite () {
return aurite;
}
#Override
public void run () {
try {
aurite = Integer.MAX_VALUE;
for (int i = 0; i < serlvulate.length; i++) {
if (i < this.aurite) {
this.aurite = i;
sleep (1000);
}
}
} catch (InterruptedException e) {
System.out.println("Found an Exception!");
return;
}
}
}
public class Stub {
public static int polytoky (int[]a, int[]b) throws InterruptedException {
Eczema Eczema1 = new Eczema (a);
Eczema Eczema2 = new Eczema (b);
Eczema1.start();
Eczema1.join();
Eczema2.start();
Eczema2.join();
return Math.min (Eczema1.getAurite(), Eczema2.getAurite());
}
}
I followed the instructions of the exercise but when I chech it on the cmd the following error occurs:
Field servulate not found: java.lang.NoSuchFieldException: servulate.
Any ideas how I fix it?
You appear to be accessing a field named servulate while the proper field name is serlvulate. Find the line where this happens and fix the spelling.
Given the following code , from some reason it won't create an instance of MyVector . What might be the problem ? The problem occurs in the line of Main :
MyVector vec = new MyVector();
However , when I create the an instance of MyVector with the other constructor :
MyVector vec2 = new MyVector(arr);
it compile and the instance is allocated.
class Dot:
public class Dot {
private double dotValue;
public Dot(double dotValue)
{
this.dotValue = dotValue;
}
public double getDotValue()
{
return this.dotValue;
}
public void setDotValue(double newDotValue)
{
this.dotValue = newDotValue;
}
public String toString()
{
return "The Dot's value is :" + this.dotValue;
}
}
class MyVector
public class MyVector {
private Dot[] arrayDots;
MyVector()
{
int k = 2;
this.arrayDots = new Dot[k];
}
public MyVector(int k)
{
this.arrayDots = new Dot[k];
int i = 0;
while (i < k)
arrayDots[i].setDotValue(0);
}
public MyVector(double array[])
{
this.arrayDots = new Dot[array.length];
int i = 0;
while (i < array.length)
{
this.arrayDots[i] = new Dot(array[i]);
i++;
}
}
}
and Main
public class Main {
public static void main(String[] args) {
int k = 10;
double [] arr = {0,1,2,3,4,5};
System.out.println("Enter you K");
MyVector vec = new MyVector(); // that line compile ,but when debugging it crashes , why ?
MyVector vec2 = new MyVector(arr);
}
}
Regards
Ron
I copied your code into my Eclipse IDE and got an "org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array." Exception when I click on the arrayDots variable.
Your code is ok and working. The debugger has a problem because the Dot class is not loaded.
See also: http://www.coderanch.com/t/433238/Testing/ClassNotLoadedException-Eclipse-debugger
You could change your Main as follows (I know this is not very beautiful)
public static void main(String[] args) {
int k = 10;
double [] arr = {0,1,2,3,4,5};
System.out.println("Enter you K");
new Dot(); // the classloader loads the Dot class
MyVector vec = new MyVector(); // that line compile ,but when debugging it crashes , why ?
MyVector vec2 = new MyVector(arr);
}
Your default constructor is not visible. Add public keyword in front of the constructor.