I have problems with these methods.
The error is:
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: pkgModelo.AnalizadorLexico: method <init>()V not found
The classes are:
Class frmAnalizador:
package pkgVista;
import pkgModelo.AnalizadorLexico;
public class frmAnalizador extends javax.swing.JFrame {
AnalizadorLexico alexico;
String linea;
JFileChooser abrirArchivo;
public frmAnalizador() {
initComponents();
alexico = new pkgModelo.AnalizadorLexico();
}
}
In object alexico show the exception.
Class AnalizadorLexico:
package pkgModelo;
import java.io.FileInputStream;
public class AnalizadorLexico implements AnalizadorLexicoConstants {
public AnalizadorLexico() {
}
public static void principal(FileInputStream file) throws ParseException {
try {
AnalizadorLexico analizador = new AnalizadorLexico(file);
analizador.Algoritmo();
System.out.println("El analizador l\u00e9xico ha compilado correctamente");
}
catch(ParseException e) {
System.out.println("Hay errores: " + e.getMessage());
}
}
}
Here in this line AnalizadorLexico analizador = new AnalizadorLexico(file); you passed file object as a parameter where as your class AnalizadorLexico has not any kind of parameterized constructor so you have to make one more constructor which has a parameter of FileInputStream.
public AnalizadorLexico(FileInputStream file){
//Your Code
}
Related
package RMI_Package;
import java.rmi.server.*;
import java.rmi.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String sayHello(){
return "Server says,'Hey'";
}
public MyRemoteImpl() throws RemoteException{}
public static void main(String [] args){
try{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello",service);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
This code is from Head First Java Book when i run it, it throws the java.net.MalformedURLException.
As specified by the Naming documentation, the first parameter of bind should be a valid URL.
As an example (taken from here):
Naming.bind("rmi://localhost:8800/YourObject", service);
Error: Main method not found in class jsone.testing, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
package jsone;
import java.io.File;
import java.io.IOException;
import org.apache.xpath.operations.String;
import org.testng.annotations.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
public class testing {
#Test
public static void main(String args[]) {
ObjectMapper mapper = new ObjectMapper();
try {
File jsonInputFile = new File("D:\\workspace\\jsone\\car.json");
car emp = mapper.readValue(jsonInputFile, car.class);
System.out.println(emp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package jsone;
public class car{
private String colour;
public String getcolour() {
return colour;
}
public void setcolour(String colour) {
this.colour = colour;
}
#Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("\n----- Employee Information-----\n");
sb.append("Colour: " + getcolour() + "\n");
sb.append("*****************************");
return sb.toString();
}
}
In your example String class comes from org.apache.xpath.operations.String package which is wrong. You should use java.lang.String class instead. Classes from java.lang package are visible in class by default so you have to delete below line:
import org.apache.xpath.operations.String;
and it should start work. Also, your main method should not be annotated with #Test annotation.
Also, please take a look on this question:
Error: Main method not found in class MyClass, please define the main method as…
Suppose that I have a .class file, can I get all the methods included in that class ?
Straight from the source: http://java.sun.com/developer/technicalArticles/ALT/Reflection/
Then I modified it to be self contained, not requiring anything from the command line. ;-)
import java.lang.reflect.*;
/**
Compile with this:
C:\Documents and Settings\glow\My Documents\j>javac DumpMethods.java
Run like this, and results follow
C:\Documents and Settings\glow\My Documents\j>java DumpMethods
public void DumpMethods.foo()
public int DumpMethods.bar()
public java.lang.String DumpMethods.baz()
public static void DumpMethods.main(java.lang.String[])
*/
public class DumpMethods {
public void foo() { }
public int bar() { return 12; }
public String baz() { return ""; }
public static void main(String args[]) {
try {
Class thisClass = DumpMethods.class;
Method[] methods = thisClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
System.out.println(methods[i].toString());
}
} catch (Throwable e) {
System.err.println(e);
}
}
}
To know about all methods use this statement in console:
javap -cp jar-file.jar packagename.classname
or
javap class-file.class packagename.classname
or for example:
javap java.lang.StringBuffer
You can use the Reflection API
package tPoint;
import java.io.File;
import java.lang.reflect.Method;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class ReadClasses {
public static void main(String[] args) {
try {
Class c = Class.forName("tPoint" + ".Sample");
Object obj = c.newInstance();
Document doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new File("src/datasource.xml"));
Method[] m = c.getDeclaredMethods();
for (Method e : m) {
String mName = e.getName();
if (mName.startsWith("set")) {
System.out.println(mName);
e.invoke(obj, new
String(doc.getElementsByTagName(mName).item(0).getTextContent()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I got this code to instantiate a Life class from jar file:
import com.life.Life;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
public class Main {
public static void main(String[] args) {
try{
File file = new File("Life.jar");
URL url = file.toURI().toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("com.life.Life");
Life life = (Life) cls.newInstance();
System.out.println("Message: "+life.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
}
Here's the content of the Life.jar
public class Life {
public String getMessage(){
return "Life is Beautiful!";
}
}
Here's my interface name Life
package com.life;
public interface Life {
public String getMessage();
}
The code above will throw an error:
java.lang.InstantiationException: com.life.Life
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at com.Main.main(Main.java:20)
BUILD SUCCESSFUL (total time: 0 seconds)
What's wrong with the code? How to resolve this?
This happened because your interface is also named Life ( java tried to instantiate an interface). Change public interface Life to public interface LifeInterface and then have your class Life implement that like :
public class Life implements LifeInterface
{
#Override
public String getMessage()
{
return "Life is Beautiful!";
}
}
This is my sample test project which tests android calculator using robotium.
I want to create jar file of this project but while creating it, it shows error as below:
"Error: Could not find or load main class TestMain".
I think its showing this error becouse there is no main class in that i.e. it couldn't find "public static void main(String args[])". what should i do to overcome this problem?
package com.testcalculator;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
#SuppressWarnings("unchecked")
public class TestCal extends ActivityInstrumentationTestCase2
{
private static final String TARGET_PACKAGE_ID="com.calculator";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.calculator.Main";
private static Class launcherActivityClass;
static
{
try
{
launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
public TestCal()throws ClassNotFoundException
{
super(launcherActivityClass);
}
private Solo solo;
#Override
protected void setUp() throws Exception
{
solo = new Solo(getInstrumentation(),getActivity());
}
public void testDisplayBlackBox()
{
solo.enterText(0, "10");
solo.enterText(1, "20");
solo.clickOnButton("Multiply");
assertTrue(solo.searchText("200"));
}
#Override
public void tearDown() throws Exception
{
solo.finishOpenedActivities();
}
}