Question

I am a beginner to Eclipse JDT. I was going through some tutorial and found one good example for creating the java file. In this below example in which project they will create the package and java file. I could not see any code pointing to any of the project name. Please make me understand if I am wrong. I just run the below example. I could not see any output..

        AST ast = AST.newAST(AST.JLS3);
        CompilationUnit unit = ast.newCompilationUnit();
        PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
        packageDeclaration.setName(ast.newSimpleName("example"));
        unit.setPackage(packageDeclaration);
        ImportDeclaration importDeclaration = ast.newImportDeclaration();
        QualifiedName name = 
            ast.newQualifiedName(
                ast.newSimpleName("java"),
                ast.newSimpleName("util"));
        importDeclaration.setName(name);
        importDeclaration.setOnDemand(true);
        unit.imports().add(importDeclaration);
        TypeDeclaration type = ast.newTypeDeclaration();
        type.setInterface(false);
        type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        type.setName(ast.newSimpleName("HelloWorld"));
        MethodDeclaration methodDeclaration = ast.newMethodDeclaration();
        methodDeclaration.setConstructor(false);
        List modifiers = methodDeclaration.modifiers();
        modifiers.add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        modifiers.add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
        methodDeclaration.setName(ast.newSimpleName("main"));
        methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
        SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
        variableDeclaration.setType(ast.newArrayType(ast.newSimpleType(ast.newSimpleName("String"))));
        variableDeclaration.setName(ast.newSimpleName("args"));
        methodDeclaration.parameters().add(variableDeclaration);
        org.eclipse.jdt.core.dom.Block block = ast.newBlock();
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        name = 
            ast.newQualifiedName(
                ast.newSimpleName("System"),
                ast.newSimpleName("out"));
        methodInvocation.setExpression(name);
        methodInvocation.setName(ast.newSimpleName("println")); 
        InfixExpression infixExpression = ast.newInfixExpression();
        infixExpression.setOperator(InfixExpression.Operator.PLUS);
        StringLiteral literal = ast.newStringLiteral();
        literal.setLiteralValue("Hello");
        infixExpression.setLeftOperand(literal);
        literal = ast.newStringLiteral();
        literal.setLiteralValue(" world");
        infixExpression.setRightOperand(literal);
        methodInvocation.arguments().add(infixExpression);
        ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation);
        block.statements().add(expressionStatement);
        methodDeclaration.setBody(block);
        type.bodyDeclarations().add(methodDeclaration);
        unit.types().add(type);
Was it helpful?

Solution

You may take a look at this. It uses Java Model, which means you will need to create a plug-in to make it work.

// create a project with name "TESTJDT"
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("TESTJDT");
project.create(null);
project.open(null);

//set the Java nature
IProjectDescription description = project.getDescription();
description.setNatureIds(new String[] { JavaCore.NATURE_ID });

//create the project
project.setDescription(description, null);
IJavaProject javaProject = JavaCore.create(project);

//set the build path
IClasspathEntry[] buildPath = {
        JavaCore.newSourceEntry(project.getFullPath().append("src")),
                JavaRuntime.getDefaultJREContainerEntry() };

javaProject.setRawClasspath(buildPath, project.getFullPath().append(
                "bin"), null);

//create folder by using resources package
IFolder folder = project.getFolder("src");
folder.create(true, true, null);

//Add folder to Java element
IPackageFragmentRoot srcFolder = javaProject
                .getPackageFragmentRoot(folder);

//create package fragment
IPackageFragment fragment = srcFolder.createPackageFragment(
        "com.programcreek", true, null);

//init code string and create compilation unit
String str = "package com.programcreek;" + "\n"
    + "public class Test  {" + "\n" + " private String name;"
    + "\n" + "}";

        ICompilationUnit cu = fragment.createCompilationUnit("Test.java", str,
                false, null);

//create a field
IType type = cu.getType("Test");

type.createField("private String age;", null, true, null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top