Java main within Tomcat project in Eclipse - java

I have a running and working Tomcat Project and I want to add a normal Java project into it for better and faster testing. No pages to get fast database outputs and such.
In the WEB-INF/src folder I have added a package with contains my main class and includes some of the other controllers from the project.
EDIT: Moved sources to the /src (as suggested) folder but still the same error.
Right now it is a simple println in a main class. The problem is that eclipse tells me it cannot find the main clas or load it. (right click on the main class file -> runs as -> Java Application)
package mytomcatproj.maintest
import mytomcatproj.othercontroller
public class MyMainClass {
public static void main(String[] args) {
String overval="te";
String overres= othercontroller.getWord(overval);
System.out.println("Test" + overres);
}
}
How can I get the project to run?
Thank you for you help.

Related

can't import class src/test/java into src/main/java in eclipse

I am using gradle in eclipse.
I want to use class in src/test/java/testing inside src/main/java/program.
I tried doing :
// in program.java
public static void main(String[] args) {
final testing t = new testing();
// in testing.java
public class testing {
... some code ...
}
but the execution failed because it couldn't find symbol "class testing".
I do not see any problem in my program, so I am assuming that I did not properly set the environment up.
I've seen a post saying I have to build path but it says there's no action available.
Can someone help me set the environment up on mac?
I want to use class in src/test/java/testing inside src/main/java/program.
No, that's not how it's supposed to go. Gradle, eclipse, or for that matter any build system and any IDE will fight you.
Test code depends on main code, but main code does not depend on test code. It wouldn't work - test code is not shipped when you deploy your application.
If you want to write some tests that you run from main, put that code in src/main itself. src/test is for automated testing purposes.
If you are making a class for some data tracking that the actual test code will use, and currently this tracking class lives in src/test because you feel that is where it should go, given that it is test related - move that class to main. Anything that src/main/... uses, is by definition 'main' code.

How to Create a Compatible JAR File for Selenium Tests?

I have created a script to perform few automated tests in Selenium for a website using Java in Eclipse.
My aim here is to create a JAR file for my automated tests so that when the file is simply executed, the tests will run on any other system configured with a Selenium environment. For that I have created a runnable JAR file from Eclipse by clicking on the Export option from the File menu. The name of the JAR file is Test MyWebsite.jar.
The source code of my Java classes is given below:
Main.java
package testproject.main;
import testproject.testmywebsite.*;
public class Main {
public static void main(String[] args) {
TestMyWebsite tmw = new TestMyWebsite();
tmw.testMyWebsite();
tmw.stopTest();
}
}
TestMyWebsite.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import testproject.testmywebsite.tools.*;
import testproject.testmywebsite.login.*;
public class TestMyWebsite {
private WebDriver driver;
public TestMyWebsite() {
setUp();
}
private void setUp() {
// Create a new instance of the Chrome driver
driver = new ChromeDriver();
driver.manage().window().maximize();
}
public void testMyWebsite() {
testLogin();
}
public void testLogin() {
TestLogin tl = new TestLogin(driver);
tl.testLogin();
}
public void stopTest() {
//Close the browser
driver.quit();
}
}
TestLogin.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestLogin {
private WebDriver;
public TestLogin(WebDriver driver) {
this.driver = driver;
}
public void testLogin() {
//Perform the login test
}
}
The problem here is that despite setting up and configuring the environment on other computers with the Selenium webdriver, copying the Test MyWebsite.jar in the third-party computer and running the file by double-clicking it, the tests do not run successfully. What happens is the Chrome browser opens momentarily and then closes without opening the 'MyWebsite's' URL. In other words, only the two lines of code in the Main.java class are executed. The JAR file is unable to find the other two associated classes in the Eclipse project. However when I execute the file in my computer the tests are perfectly run, which means that there is a problem with how I have created the JAR file. Please note that these are just three classes of the 12 classes I have created in 5 different packages in my Eclipse project to make understanding easier.
Can anyone please tell me where am I going wrong in creating my JAR file which will run both on my computer as well as on another computer configured with the Selenium environment? As a beginner in Selenium I had followed these instructions while creating my Selenium project in Eclipse. Replies at the earliest will be highly appreciated.
Thank you.
Okay I found out the solution to the problem myself.
The problem lied in the way I was creating the Runnable JAR file in Eclipse. I was selecting the Extract required libraries into generated JAR option (selected by Eclipse by default) under the Library handling label on the first page. By doing so it was not packing the required external Selenium, TestNG and JRE System Library, JAR file libraries in my runnable JAR, hence it was not able to find the required classes. My JAR started working perfectly after selecting the second option, Package required libraries into generated JAR.
For one thing, you should use a test runner, such as JUnit or TestNG, to run your tests. Another thing is to make sure you are using Maven and conforming to its 'standard directory layout'. By using Maven, you will be able to run the 'install' or 'package' goal/task to create the standalone 'executable jar'. Keep in mind that you will need to configure Maven in a unusual way , where you tell it to include all files under 'src/main/java' and 'src/test/java' into the same single .jar file. Also, you will need to configure Maven in such a way as to create a .jar that is actually executable.

Small TestNG app won't run correctly

I created a Java Project in Eclipse and ran some TestNG files successfully. Now I've created a new Java Project and find out my TestNG files won't run as expected.
So, to troubleshoot, I created a very simple test2.class file shown below for my first (previous) Java Project and the same file for my new java Project (top line changed to reflect package name). When I execute this file as a TestNG app, it runs fine in my first project (prints out 'Test' in the console window). However, when I run it in the same way in my new project, nothing prints out and I don't see any errors.
I checked my Java Build Paths and they are the same for both. So, obviously I am missing something in my new java project???
package com.selftechy.seltests;
import org.testng.annotations.Test;
public class Test2 {
#Test
public void Test() {
System.out.println("Test");
}
}
Just a shot in the sky...
If this is your actual code listed, it has a compilation error because of the missing '}' in the method. So the TestNG execution does not even start.

Java: Eclipse: Cannot Find Main Class

I had a project that was compiling and running fine. I had also exported the build.xml file without issue. However, today, I clicked Project --> Clean..., and the project will no longer run. It raises the error:
Error: Could not find or load main class com.bar.Foo
Is there any way I can undo this?
The answer is probably no, you cannot "undo" this. You can probably fix it by building your project, making sure that the project/class exists and that it contains a public static void main(String ... args) method.
Perhaps the file that you're trying to run was removed from the src folder.

Exporting Scala project as jar from Eclipse

I have a Scala project and I would like to export it as a jar.
*1. At first I tried creating a Java class for the project as an entry point
public class JMain {
public static void main(String[] args) {
System.out.println("Java main calling Scala main");
SMain.main(new String[] {""}); //SMain.main is the actual *main*
and this worked fine and dandy when launched from Eclipse, but when I export it as jar it'll give me 18 exceptions or so. I do now know how to replicate then "environment" in which Eclipse manages to launch this and I'm prety sure it relies on the fact that Scala is on my system already - I need a self contained jar with everything packed in there.
*2. My second try consisted of trying what lach suggested here How to deploy a Scala project from Eclipse?
namely:
public class JMain {
public static void main(String[] args) {
System.out.println("Java Main");
List<String> argList = new ArrayList<String>();
argList.add("fully.qualified.ClassName"); //???
for (String s : args) argList.add(s);
scala.tools.nsc.MainGenericRunner.main(argList.toArray(new String[0]));
This time it won't even run from Eclipse, although it gives only 6 or so exceptions starting with the famous NoClassDefFoundError. I have a feeling I'm not getting fully.qualified.ClassName right. *3. If the main Scala class is called "Dis.scala" and is located in package "pack" shouldn't this fully.qualified.ClassName be "pack.Dis"?
I'm using Jre 1.6 and Scala 2.9.2
EDIT: I have included all external imported jars, even scala-library.jar - everything is nice and packed in the jar
P.S. I am not familiar with Ant or Maven or Sbt. I just want my Scala project jared - if possible without getting into hairy things.
Here is what worked for me:
1. Create scala project
2. Create Wrapper java project
3. Add the scala-library.jar to you java project build path.
So you only need the 3rd step in addition since the rest looks similar to what I did. Then you can happily use: java - jar file.jar
EDIT:
How to create a JAR File which contains Scala/Code which can be consumed by another Java Project, using Scala - Eclipse IDE.
Create a new Scala Project and define an object with a main method as entry point.
Now create a new Java Project and add your Scala Project to the new ones buildpath. Additionally add the scala-library.jar to the Java project.
Now create a Wrapper class in the java project which calls your entry point class from the scala lib. Run the wrapper class to create a eclipse run configuration and test if you can call the scala project.
Use the Export->Java->Runnable JAR file, Wizard now on the wrapper project.The eclipse run configuration will be used as entrypoint into the JAR. Depending on your needs you may want to :
extract required libraries into generated JAR
or
Package required libraries into generated JAR
Finally you get a complete packaged JAR which you can use like this:
java - jar wrapped.jar
For me, it was relatively straightforward.
Develop and test the project using the scala IDE (or eclipse for java).
once ready, generate the jar for the project using file -> export method.
for submitting the spark (i was writing something for spark), i just had to mention --class option for specifying the main class for the jar.
hope to help.

Categories