How do I reference a jar from an eclipse project? - java

I have some code copied from the fitness website:
package fixtures;
import static fitnesse.util.ListUtility.list; //fitnesse.util can not be resolved
import static util.ListUtility.list; //this one resolves but is no help in getting list to work below
import java.util.Date;
import java.util.List;
public class EmployeesHiredBefore {
private Date date;
public EmployeesHiredBefore(Date date) {
this.date = date;
}
public void table(List<List<String>> table) {
//optional function
}
public List<Object> query() {
return
list(
list(
list("employee number", "1429"),
list("first name", "Bob"),
list("last name", "Martin"),
list("hire date", "10-Oct-1974")
),
list(
list("employee number", "8832"),
list("first name", "James"),
list("last name", "Grenning"),
list("hire date", "15-Dec-1979")
)
);
}
}
I have added to build path with add external jar of fitnesse.jar its contents includes util/ListUtility.class
Anyone know how to reference this jar?

The current version of fitnesse.jar (from http://fitnesse.org/FrontPage.FitNesseDevelopment.DownLoad) has a util.ListUtility class but not fitnesse.util.ListUtility.
So you (probably) want
import static util.ListUtility.list;
but what problem are you actually getting when you use this import? In what way do things not work?

In the fitnesse jar there is no package called "fitnesse.util".
Recheck.
I could find only
util.ListUtility.list;

I am using fitnesse version - release 20150114.
There is no ListUtility under following:
Util package
fitnesse.util package
I resolved it by creating ListUtility class in source by followng way.
Fitnesse importing util.ListUtility.list; is giving error

Related

How Do I Import Classes From Another Package and Folder in Java?

I just started studying OOP and Packages in Java. I have a question regarding package importing in Java.
I have two files, named ImportThis.java and Here.java
The directory for ImportThis.java on my local machine is F:\VS Codes\master\folderone\folderoneone\ImportThis.java. And the contents of ImportThis.java is:
package master.folderone.folderoneone;
public class ImportThis {
public static void aStaticMethod() {
System.out.println("Hello World");
}
}
The directory for Here.java on my local machine is F:\VS Codes\master\foldertwo\foldertwotwo\Here.java. And the contents of Here.java is:
package master.foldertwo.foldertwotwo;
public class Here {
public static void anotherMethod() {
ImportThis.aStaticMethod();
}
}
By looking at the contents of Here.java, you might be able to tell that I want to import the class ImportThis from ImportThis.java to Here.java, and it is indeed what I've been trying to do. But both ImportThis.java and Here.java came from different folders and packages. I've tried using import master.folderone.folderoneone.ImportThis; on Here.java but VS Code says it cannot be resolved. Looking forward to the answer for my question!
EDIT: Changed package names and lowercased the folder names
Try:
package master.foldertwo.foldertwotwo;
import master.folderone.folderoneone.ImportThis; //<-import statement
public class Here {
public static void anotherMethod() {
ImportThis.aStaticMethod();
}
}

call method java from a maven project in eclipse

I have two maven projects "Bonita-engine"and "activity-engine". these projects are the code source of two BPM engine. My project is to find the common method of these two engine code sources.So I create an API Java to call this Java method.
as these methods are on a different project I can not call this method. in fact, I added this two project to the library of my API Java but it doesn't work.
call method getDescription() from bonita-engine maven project
* Copyright (C) 2015 BonitaSoft S.A.
package org.bonitasoft.engine.bpm.process.impl.internal;
import java.util.Date;
import org.bonitasoft.engine.bpm.internal.NamedElementImpl;
import org.bonitasoft.engine.bpm.process.ProcessInstance;
/**
* #author Baptiste Mesta
* #author Matthieu Chaffotte
* #author Celine Souchet
*/
public class ProcessInstanceImpl extends NamedElementImpl implements ProcessInstance {
#Override
public String getDescription() {
return description;
}
}
call method getDescription() from activiti-engine maven project
package org.activiti.engine.impl.persistence.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.impl.bpmn.data.IOSpecification;
import org.activiti.engine.impl.context.Context;
public class ProcessDefinitionEntityImpl extends AbstractEntity implements ProcessDefinitionEntity, Serializable {
public ProcessInstanceImpl(final String name) {
super(name);
}
public String getDescription() {
return description;
}
}
API JAVA : call the common method from the two maven project
import org.activiti.bpmn.model.*;
import org.bonitasoft.engine.bpm.process.impl.internal.* ;
import org.bonitasoft.engine.bpm.*;
import java.util.*;
import java.util.Date;
import org.bonitasoft.engine.bpm.internal.*;
import org.bonitasoft.engine.bpm.process.* ;
public class apicommon {
public activitiProcess = new ProcessDefinitionEntityImpl() ;
public String name;
public bonitaProcess = new ProcessInstanceImpl(name) ;
public enum bpm {
activiti , bonita
}
bpm chose ;
public apicommon() {
}
public String getProcessDescription() {
if(chose==bpm.activiti){
return activitiProcess.getDescription() ;
}else if(chose==bpm.bonita){
return bonitaProcess.getDescription();
}
}
i import the package "org.bonitasoft.engine.bpm.process.impl.internal" and the package "org.activiti.engine.impl.persistence.entity" but i can not get access to ProcessInstanceImpl method and ProcessDefinitionEntityImpl method !
#sara, you have to add the 2 projects to the build path of the API java(api common).
Right click on the (api common) java project => Build path => Configure Build Path
Click on the projects tab
Click Add button
add your project1 (bonita) by checking the box next to it
Again add project2 (activiti-engine) usign the same method
Click Apply and OK to close the dialog.
Now your imports should be working.
Edit:
You have not been clear about which import is the problem. More information is necessary to get to the root of the problem.
As regarding the import error, you can Ctrl + click the offending import. This will take you to a page Source Not Found and a button labelled Attach Source. Click the button to search for the location of what I am suspect is an external jar.

Getting Netbeans Java program to compile

I'm new to java, and I've been trying to get my program to compile using Netbeans. HelloWorldApp.java uses the Greeter class in Greeter.java. I keep getting errors and I can't figure it out. I understand that you have to include "packages" or something. I don't have a lot of experience with Netbeans either. But I would love for this to work.
Here is the HelloWorldApp.java:
package helloworldapp;
import Greeter
public class HelloWorldApp
{
public static void main(String[] args)
{
Greeter myGreeterObject = new Greeter();
myGreeterObject.sayHello();
}
}
And here is Greeter.java:
public class Greeter
{
public void sayHello()
{
System.out.println("Hello, World!");
}
}
Change the first line of Greeter to
package helloworldapp;
And then remove
import Greeter
from HelloWorldApp. You only need to import classes that are in other packages. Also, an import line is terminated with a semicolon. Finally, import is always optional and a convenience for the developer; as an example,
import java.util.Calendar;
Allows you to write
Calendar cal = Calendar.getInstance();
But, without the import you could still use
java.util.Calendar cal = java.util.Calendar.getInstance();
Just put the Greeter class in the same folder (i.e. package) as the other file and remove the "import Greeter" statement. You should put every class in a package as you did with the HelloWorldApp class.
If you leave classes without package (i.e. in the root folder) you cannot import them.
As long as both are in the same package (folder) there will be no need for the "import Greeter" statement, this should fix it, hope this helps!

Play Framework Does Not Create Models

I just downloaded the play framework from their site and am working through this tutorial.
I've noticed the framework creates the folders app/controllers and app/views, but not a models folder. I created it manually and added Task.java to it. When I get to the section entitled "Rendering the first page" and open localhost:9000/tasks I get a compilation error that says package play.models does not exist. Here is what my Task.java looks like:
package models;
import java.util.*;
public class Task {
public Long id;
#Required
public String label;
public static List<Task> all() {
return new ArrayList<Task>();
}
public static void create(Task task) {
}
public static void delete(Long id) {
}
}
Here is application.java, the file generating the compilation error:
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
import play.data.*;
import play.models.*; // COMPILATION ERROR: "package play.models does not exist"!
public class Application extends Controller {
static Form<Task> taskForm = Form.form(Task.class);
public static Result index() {
//return ok(index.render("Your new application is ready."));
return redirect(routes.Application.tasks());
}
public static Result tasks() {
return ok(views.html.index.render(Task.all(), taskForm));
}
public static Result newTask() {
return TODO;
}
public static Result deleteTask(Long id) {
return TODO;
}
}
I believe it's supposed to be import models.Task; as opposed to import play.models.*;
That's quite confusing (IMHO) step in this tutorial, instead scroll down to Persist the tasks in a database section which describes preparing a model to cooperate with DB :) (it extends Model class, uses proper annotations, etc)
As you recognized it yet, you need to create a models package yourself.
Also as cYn wrote: you should import models like models.SomeModel into your controller
You are correct HukeLau_DABA , the Play will not create the models package for you. you have to create it.
I got these imports in my Application controller class. I got this sample play application running.
import play.api._
import play.api.mvc._
import play.api.data.Form
import play.api.data.Forms._
import models.Task
and another thing in Eclipse is it will not import the necessary imports automatically.
it is bit pain now, once the IDE support get better I hope this will change.

How to get all imports defined in a class using java reflection?

Hi i am new in java reflection domain.So can anyone guide me in this problem scenario.
I have a class named "SomClass.java" and it imports a package named "SomPackage.RefClass" And some other java libraries like java.lang.. etc.
Now i wish to get know all the imports defined in a class through reflection.
import SomPackage.RefClass;
import java.lang.reflect.Field;
import java.io.IOException;
public class SomeClass{
RefClass refClass_Obj;
String nationality;
///some other members
}
I just wanna know the list of all import defined in a class using reflection.
I have seen a Question posted hear similar to my Q but it is not well elaborated so,need some good direction of help.
thanks in advance.
I just wanna know the list of all
import defined in a class using
reflection
You can't because the compiler doesn't put them into the object file. It throws them away. Import is just a shorthand to the compiler.
Imports are a compile-time feature - there's no difference to the compiled code between a version which uses the full name of the type everywhere it's mentioned, a version which imports everything using a *, and a version which imports classes by full name.
If you want to find all the types used within the compiled code, that's a slightly different matter. You may want to look at BCEL as a way of analyzing bytecode.
I think you can use Qdox to get all the imports in a class which is not actually through reflection, but it can serve your purpose :
String fileFullPath = "Your\\java\\ file \\full\\path";
JavaDocBuilder builder = new JavaDocBuilder();
builder.addSource(new FileReader( fileFullPath ));
JavaSource src = builder.getSources()[0];
String[] imports = src.getImports();
for ( String imp : imports )
{
System.out.println(imp);
}
As suggested by #Asraful Haque qdox helps to read imports of java file
Use Maven dependency
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>2.0.1</version>
</dependency>
Please refer modified version of code
package readimports;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Collection;
import java.util.List;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaSource;
public class TestReadAllImport {
public static void main(String[] args) throws FileNotFoundException {
String fileFullPath = "path to java file";
JavaProjectBuilder builder = new JavaProjectBuilder();
builder.addSource(new FileReader( fileFullPath ));
Collection<JavaSource> srcs = builder.getSources();
for(JavaSource src : srcs) {
List<String> imports = src.getImports();
for ( String imp : imports )
{
System.out.println(imp);
}
}
}
}
Thanks for sharing the qdox, I used it to recursively find all imports and find unique packages and unique imports.
<!-- https://mvnrepository.com/artifact/com.thoughtworks.qdox/qdox -->
<dependency>
<groupId>com.thoughtworks.qdox</groupId>
<artifactId>qdox</artifactId>
<version>2.0.3</version>
</dependency>
Using simple recursion to get all packages and imports of the given class.
package test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaSource;
public class ImportsIdentifier {
private static String sysPath ="//<Absolute Path>/src/main/java/";
private static String fileType = ".java";
private static Set<String> importFiles = new HashSet<>();
private static Set<String> packages = new HashSet<>();
public static void main(String[] args) throws FileNotFoundException {
String path = sysPath + "<java file path>";
printImports(path);
System.out.println(importFiles);
System.out.println(packages);
}
private static void printImports(String path) throws FileNotFoundException {
JavaProjectBuilder jp = new JavaProjectBuilder();
jp.addSource(new FileReader(path));
Collection<JavaSource> srcs = jp.getSources();
for (JavaSource src : srcs) {
System.out.println(src.getPackage());
packages.add(src.getPackage().toString());
for(String imprt: src.getImports()) {
if(imprt.startsWith("<filter for any package>")) {
imprt = sysPath+imprt.replaceAll("\\.", "/")+fileType;
if(importFiles.contains(imprt)) {
continue;
}
importFiles.add(imprt);
System.out.println(imprt);
printImports(imprt);
}
}
}
}
}

Categories

Resources