Could not find main method from given launch configuration - eclipse

I've a simple Java project that works when I execute it at Eclipse environment. But when I try to export it to a Runnable Jar, I get the following error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: JavaSwing/src.main.java/com/cansoft/GUIProgram.java
Exported with compile warnings: JavaSwing/src.main.java/com/util/Util.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
I read other posts which suggest to create a MANIFEST.MF file specifying the main-class which I did. It is placed at MyProjectFolder/META-INF/MANIFEST.MF and it contains the following information:
Manifest-Version: 1.0
Class-Path: resources
main-class: com.cansoft.GUIProgram
My main class is as follows:
public class GUIProgram {
private JFrame folderCreationSubappFrame;
private Color color;
private String home;
private final static Logger LOG_MONITOR = Logger.getLogger("com.cansoft");
public static void main(String[] args) {
try {
new GUIProgram();
} catch (Exception e) {
LOG_MONITOR.log(Level.INFO,e.getMessage());
}
}
public GUIProgram() throws InterruptedException, SecurityException, IOException {
home = System.getProperty("user.home") + File.separator + "Documents";
startLogSystem();
if(isFirstRun()) {
showWelcomeFrame();
} else {
initialize();
}
} .... More and more code
Does anybody know what am I missing? Any help much appreciated.
Thank you.

It is not enough to create the manifest file, you need to explicitly choose it in the Eclipse jar export dialog.
Answer to Comment
If you use "runnable jar", make sure that you chose the correct launch configuration and that the launch configuration successfully runs when chosing "Run As" -> "Run Configurations" -> "Java Application" -> Your Configuration -> "Run"

I finally find out where the problem was, it was quite simple btw. I had created my GUIProgram within a src.main.java package, but that package was created (my bad) as resources instead of folders, so Eclipse was smart enought to run it but when trying to generate the JAR which expected a correct java project structure, it was failing because truly there were not GUIProgram java class at src path (src was not folder type but resources).
Hope I succeed explaining.

Related

Export Selenium TestNG script from Eclipse to runnable jar

I have a script written in Selenium using the TestNG framework. This script executes via an xml file. Since we do not have main method in this, how can I export this script to a runable JAR file?
I marked this question as a dupe (and this code snippet comes from the dupe question), but basically you could write a public static void main method that scans for tests or loads a existing testng.xml file. Then, you just need to use something such as Maven Shade plugin to package it.
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { test_start.class });
testng.addListener(tla);
testng.run();
}
I used WebDriverManager project to manage my driver libraries. I would have the .jar load those externally.

Not able to create executable jar for webdriver+TestNg project which does not contain main method in any class

I am learning webdriver and I have created one project with TestNg.
I have several classes in my package under src folder.
No class contains public static void main(....). i.e[Entry Point]
My question is :
Can we create Runnable / Executable jar file through eclipse for projects like this[project without main method]. I searched on many sites but unfortunately didnt get solution.
Please suggest me some links OR The way by which we can do this.
To create a jar file of the TestNG without main method you have to create another class which contain main method.
Suppose you have a TestNG file name as Sample.java, in the same package create one more class as ExecutableRar and write the below code :
public class ExecutableRar {
public static void main(String[] args) {
TestNG testng = new TestNG();
Class[] classes = new Class[]{Sample.class};
testng.setTestClasses(classes);
testng.run();
}
Now you can run this class as a Java Application. Then right click on the package --> Export --> Java --> Runnable jar File --> select ExecutableRar in launch configuration --> Browse the file location and enter the name of the file in Export Destination --> Finish.
Please let me know if you are having any issues.

Importing greenDAO from eclipse to Android studio 1.0 RC 2 [duplicate]

I'm looking for a clear step-by-step explanation on how to import GreenDao in Android Studio.
I've used it before in AS, but failed to get it to work again.
There are some tutorials out there, but they don't seem to apply to the latest version of AS.
When I clone from github, I get a example project stuff etc.
Is there a way to install GreenDaoGenerator without these extras?
Just looking for an up-to-date step-by-step explanation.
Update: I suggest using Realm.io now! Check it out! :-)
Any help would be appreciated!
Tested on Android Studio 2.0
With Android Studio 0.6.1+ (and possibly earlier) you can easily add non android project to your android project as a module.
Using below method you can have Java modules(greenDaoGenerator) and Android modules in the same project and also have the ability to compile and run Java modules as stand alone Java projects.
Open your Android project in Android Studio. If you do not have one,
create one.
Click File > New Module. Select Java Library and click Next.
Fill in the package name, etc and click Finish. You should now see a
Java module inside your Android project.
Open the build.gradle file of the java project and add the following dependency
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('de.greenrobot:DaoGenerator:1.3.0')
}
Copy your DaoGenerator classes or create if you don't have one to your java module.For e.g. I have created ExampleDaoGenerator class in my java module.
public class ExampleDaoGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1000, "de.greenrobot.daoexample");
addNote(schema);
new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");
}
private static void addNote(Schema schema) {
Entity note = schema.addEntity("Note");
note.addIdProperty();
note.addStringProperty("text").notNull();
note.addStringProperty("comment");
note.addDateProperty("date");
}
}
Now, to generate the classes that you can use in android project follow below steps.
Click on the run menu in the top bar. Click Edit Configurations...
In the new window, click on the plus sign at the top left of the window and select Application
A new application configuration should appear, fill the following information.
Give it a name e.g. greenDao.
In main class click … button and select your generator class which have the main method.for e.g. in this case it is
com.greendao.generator.ExampleDaoGenerator
In working directory select path of your java project.
In use class of module select you java project.
click ok.
Again go to run menu and now you can see e.g. run greendao. click on it.It should compile successfully.
Its done !!! you can check your generated classes in the folder that you have specified.For e.g. in this case it is /DaoExample/src-gen
NOTE: You can run your android project again by clicking on run menu -> Edit Configuration . select your project and click ok.
Here's a step by step overview for Integrating GreenDao into your Android Project.
[ Reference How to use GeenDao with Android ? ]
[Project Link: GreenDao Example ]
PART1 : Setting Up GREENDAO
Create an android project.
Click File >New > New Module. Select Java Library and click Next.
Now we have to add the following Gradle Dependencies.
In build.gradle of Module:app, insert
compile 'de.greenrobot:greendao:2.1.0'
In the build.gradle of Module:greendao-generator, insert
compile 'de.greenrobot:greendao-generator:2.1.0'
Make sure, you sync your project.
Now in the MainGenerator.java,
we will define the database structure.
import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;
public class MainGenerator {
public static void main(String[] args) throws Exception {
//place where db folder will be created inside the project folder
Schema schema = new Schema(1,"com.codekrypt.greendao.db");
//Entity i.e. Class to be stored in the database // ie table LOG
Entity word_entity= schema.addEntity("LOG");
word_entity.addIdProperty(); //It is the primary key for uniquely identifying a row
word_entity.addStringProperty("text").notNull(); //Not null is SQL constrain
// ./app/src/main/java/ ---- com/codekrypt/greendao/db is the full path
new DaoGenerator().generateAll(schema, "./app/src/main/java");
}
}
Run MainGenerator.java
After running this, you will observe a newly created folder i.e. db in the Main Project Folder.
PART2 : Integrating it with Android Project
Set the activity_main.xml layout.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textData"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:id="#+id/textSave"
android:layout_below="#+id/textData"
android:layout_alignEnd="#+id/textData"
android:layout_marginTop="22dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Top"
android:id="#+id/textTop"
android:layout_below="#+id/textSave"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
In MainActivity.java,
Add the following codes
package com.codekrypt.greendao;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.codekrypt.greendao.db.DaoMaster;
import com.codekrypt.greendao.db.DaoSession;
import com.codekrypt.greendao.db.LOG;
import com.codekrypt.greendao.db.LOGDao;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//Dao --> Data Access Object
private LOGDao log_dao; // Sql access object
private LOG temp_log_object; // Used for creating a LOG Object
String log_text=""; //Entered text data is save in this variable
private final String DB_NAME ="logs-db" ; //Name of Db file in the Device
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialise DAO
log_dao=setupDb();
//Setting up form elements
Button textSave= (Button) findViewById(R.id.textSave);
Button textTop= (Button) findViewById(R.id.textTop);
final TextView textData=(TextView) findViewById(R.id.textData);
assert textSave != null;
textSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
log_text=textData.getText().toString();
temp_log_object=new LOG(null,log_text);// Class Object, Id is auto increment
SaveToSQL(temp_log_object);
}
});
assert textTop != null;
textTop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textData.setText( getFromSQL() );
}
});
}
//---------------------------------SQL QUERY Functions-----------------------------------------//
public String getFromSQL(){
List<LOG> log_list = log_dao.queryBuilder().orderDesc(LOGDao.Properties.Id).build().list();
//Get the list of all LOGS in Database in descending order
if(log_list.size()>0) { //if list is not null
return log_list.get(0).getText();
//get(0)--> 1st object
// getText() is the function in LOG class
}
return "";
}
public void SaveToSQL(LOG log_object) {
log_dao.insert(log_object);
}
//----------------------------***END SQL QUERY***---------------------------------------------//
//-------------------------------DB Setup Functions---------------------------------------------//
//Return the Configured LogDao Object
public LOGDao setupDb(){
DaoMaster.DevOpenHelper masterHelper = new DaoMaster.DevOpenHelper(this, DB_NAME, null); //create database db file if not exist
SQLiteDatabase db = masterHelper.getWritableDatabase(); //get the created database db file
DaoMaster master = new DaoMaster(db);//create masterDao
DaoSession masterSession=master.newSession(); //Creates Session session
return masterSession.getLOGDao();
}
//-------------------------***END DB setup Functions***---------------------------------------//
}
Before Running the App, Make sure you have changed your configuration.
Now Run it.
PART 3 – VIEW THE SQL DB
Open Command Prompt.
Enter the following commands.
Opening the db file in SQLite3
Using SQLite3
PART 4 – EXTRAS
Structure (Core Classes) of GREENDAO
I have used this tutorial for Android Studio 0.8.9 and everything works fine.
Works on Android 1.3 Preview
For the top answer ( Tested on Android Studio 1.0 ), you might need to include that source folder in your project. Go to app/build.gradle
add the following inside android block
sourceSets{
main{
java{
srcDir 'src-gen'
}
}
Solution: IO-Exception
Go to the build from your dao generator.
add: apply 'application'
add: mainClassName = "you.package.include.Main"
execute "run" in application task (gradle task)
I dont know why it doesnt work when you create manually a run configuration.
Basically, what you need is to add a Java library module (File > New > New module..) to your Android project (assuming you're using Android Studio), and insert the generation code inside public static void main(String[] args) {} in this module's .java class. Then Run it and the code will be generated in you main app's module.
See this blog post for a step by step tutorial with explanation.

Eclipse Unresolved compilation

I have this really weird problem working on a bigger project in Eclipse Indigo 3.7.2.
I checked out the project from an SVN repository using the Subclipse plug-in and when I start the application I get the following error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at anares.preprocess.StanfordParser.getInstance(StanfordParser.java:73)
at anares.start.Startconsole.<init>(Startconsole.java:22)
at anares.start.Startconsole.main(Startconsole.java:52)
This is what Startconsole.class looks like, containing the main method:
package anares.start;
import java.io.FileNotFoundException;
import java.io.IOException;
import anares.core.AnaResAlgorithm;
import anares.preprocess.MorphaDornerSentenceSplitter;
import anares.preprocess.CollectionEquipper;
import anares.preprocess.ParserHandlerInterface;
import anares.preprocess.Preprocessor;
import anares.preprocess.SplitterInterface;
import anares.preprocess.StanfordParser;
import anares.text.AnaResTextObject;
public class Startconsole {
public final ParserHandlerInterface parserint = StanfordParser.getInstance();
public final SplitterInterface splitterint = MorphaDornerSentenceSplitter.getInstance();
public final CollectionEquipper equipperint = null;
public final static int buffersize = 5;
private Startconsole(String file) throws IOException {
AnaResTextObject object = startPreprocess(file);
startAlgorithm(object);
}
private AnaResTextObject startPreprocess(String file) throws IOException {
Preprocessor prepro = new Preprocessor(parserint, splitterint,
equipperint);
AnaResTextObject textObject = prepro.preprocessText(file);
return textObject;
}
private void startAlgorithm(AnaResTextObject object) {
AnaResAlgorithm algo = new AnaResAlgorithm(buffersize);
algo.resolveAnaphora(object);
}
public static void main(String args[]) throws FileNotFoundException,
IOException {
if(args.length > 0){
Startconsole console = new Startconsole(args[0]);
}else{
Startconsole console = new Startconsole("Text.txt");
}
}
}
As I was saying this is a bigger project and therefore contains a few .jar-files and references to other packages.
This problem only occurs on my laptop. On my other PC everything works fine, and a fellow student of mine, who works on the same project, does not have any problems either.
I already tried checking the project out again, cleaning it up and even reinstalling eclipse.
Now here's the weird part: If I comment out the whole main method, just leaving something like
public static void main(String args[]) throws FileNotFoundException,
IOException {
// if(args.length > 0){
// Startconsole console = new Startconsole(args[0]);
// }else{
// Startconsole console = new Startconsole("Text.txt");
// }
System.out.println("Hello World!");
}
I still get the exact same error message with the exact same line numbers. And no "Hello World!" in the output.
Does anyone have any ideas where the problem comes from?
Your issue seems like either there is an error in the code that I cannot see, or your Eclipse instance/compiler got into a strange state it cannot recover from.
Just some basic ideas to check
Have you tried restarting Eclipse?
Are you using the same version of Java on all computers? E.g. there might be some incompatibilities between Java 6 and Java 7.
Is automatic build turned on? Look in the Project/Build automatically menu item. It is possible that the automatic Java builder got turned off, and thus it does not recompile your code.
Have you tried to clean your project to force a rebuild? (Project/Clean menu item).
Is JDT installed in your Eclipse instance? It should be, but it might worth check for such trivial issue.
Maybe you should try to create a new workspace, and checkout the projects again.
You could also try to download Eclipse again with this new workspace idea.
If neither of these things work, I have no idea what to look for.
Look in Eclipse's Problems view (tab); any compilation problems in the project will be reported there. You can double-click on an error or warning in the Problems view and the editor will open on the specific line that is a problem.
Do one thing just remove the build path of englischPCFG.ser.gz from your project because i am sure this is not the jar file you have added in your project

Eclipse how to reference file in a different src under the same project

My current setup in eclipse is like this:
trunk
--working/src
--resources
I have a java file inside a package under working/src and I am trying to retrieve a file in the resources. The path I am using is "../resources/file.txt". However I am getting an error saying that the file does not exist.
Any help would be appreciated thanks!
Considering your structure
I have package like
trunk
working/src/FileRead.java
resources/name list.txt
Following Code might solve your problem
package working.src;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class FileRead {
public FileRead() {
URL url = getClass().getResource("/resources/name list.txt");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String nameList;
while ((nameList = in.readLine()) != null) {
System.out.println(nameList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new FileRead();
}
}
Files will be referenced relative to your project path, so use "resources/file.txt" to reference the file.
However, if you want the file to be accessible when you export the program as a JAR, the path "resources/file.txt" must exist relative to your JAR.
It depends on how you have specified your Java Build Path inside eclipse. I have tested two setups with different results:
Define the directory working/src only as build path. You can get the information what is in your build path through: Select project > Properties > Java Build Path > Tab Source. There are all source folders listed. You see there that there is a default output folder defined. All resources that are contained in a build path are copied to the default output folder, and are then available for the eclipse classes there. You can check that in the resource perspective in your bin folder if the resource is copied there. In this setup, only the classes are generated, resources are not copied (and therefore not found).
Define the directory working/src and resources as build path. Then the resource is copied and may then found by the path "file.txt"
So eclipse has a simple build process included and hides that from the developer, but it is a build process nonetheless.