I'm trying to run a small Play application (well, the tests really) in Eclipse and I've come up against a frustrating problem. When I run the command from the command line all the tests are successful, but from Eclipse I've found that it is unable to load some of our required properties files. Looking at the target directory I see two compiled class folders, classes and classes_managed. The properties files are in the classes directory, as I'd expect, but when I look to the class that's attempting to load the properties file the classpath appears to only have the classes_managed directory, which doesn't contain the properties files that are required.
For reference I am running these as JUnit tests so that I can debug them. I've run play eclipse on the command line and imported the project to Eclipse as suggested in the documentation. I'm using Eclipse Kepler and Play 2.2.3.
Any help would be appreciated.
If those files are not getting added to the classpath properly, you can ensure they're accessible by specifically telling sbt that they need to be on the classpath with:
unmanagedClasspath in Test <+= baseDirectory map { bd => Attributed.blank(bd / "conf") }
put this line somewhere in your build.sbt file
Related
I try to use JUnit tests in a Java project in VS Code.
My setup
1) JDK 17
2) Via the Extension Pack for Java I have a its extensions installed including the following:
Language Support for Java by Red Hat
Debugger for Java
Java Test Runner
3) I went to the Testing pane and downloaded (via the wizard) the testing JARs hamcrest-core-1.3.jar and junit-4.13.2.jar into the lib directory.
4) I made sure in Classpath Configuration the two JARs are listed under Referenced Libraries.
5) The project contains a main unqualified App.java with a main method and a qualified package like org.mydomain.calculations. The method(s) which I want to test are in the qualified package!
What I did then
In a class of the qualified package I right-click and choose from the context menu Source Actions..., then Generate Tests.... From the appearing project chooser I select the current project -- and then I get the error ballon "Cannot find a valid classpath entry to generate tests".
This confuses me, because I did add the JUnit JAR to the classpath.
What else can I do to make generating tests in the qualified package work?
Please install all extensions in the Extension Pack for Java and use Clean the java language server workspace in the command palette to clean up the Java workspace.
Follow the steps below to implement a minimal test example in vscode.
Open the command palette(Ctrl+Shift+P) and create a java project
Add an AppTest.java file
Select Enable Java Tests in the Testing panel
select JUit
It will automatically download third-party packages. The tests can then be run.
I am trying to learn how to build applications using Gradle; to do this I use the 'Application' plug in. This plug in builds executable scripts that runs my jar file. However the batch file that is generated is showing an error. This is for every project I build with Gradle.
I can execute the jar file fine using 'java -jar app.jar' but the app.bat file only shows the error.
The error shows is;
Error: Could not find or load main class com.test.Main
I have exhausted my ability to search stackoverflow and google for a solution. It seems possible that there is an error with the %CLASSPATH%?
Note: I had the batch file for an early project working perfectly fine. When revisiting the project later in the day (no code changed) the batch file fails to run. This makes me thing there is a setting/ environment issue more than a programmatic one?
I needed to use the assembleDist task to create a zip folder. This zip folder contained the sh and batch files with the correct classpaths and worked perfectly.
Note: The sh and batch files located in /scripts are used by Gradle to run the application during development. When trying to execute them from the terminal or command prompt they could not find the main class or the libraries used. As far as I can tell this was due to the %CLASSPATH% not being tailored for use outside of gradle.
Running the distribution task creates an archive of the project containing all of the required files and working execution scripts. These were the files I should have been testing, not those found in the gradle project.
So i have a maven project in eclipse that i can normally start via run as> Java Application. If i however try to start the class file from the target directory from the commandline via "java TestServer" it wont work. It says it cant find or load the main class. If i replace the sourcecode with a simple dummy hello world and execute then it works fine.
The code has two maven dependencies.
Simple dummy:
public class HelloWorld {
public static void main(String[] args){
System.out.println("hello world!");
}
}
Why does this happen? Is there a problem due to the nestest class? Why can't a main class be found for the code but for a small dummy?
The problem is the classpath.
Maven is not responsible for running the project from Eclipse workspace
because Eclipse holds its own .project file, which contains all classpath entries. When you use Run As, Eclipse just uses all classpath configurations of its .project file.
To start the java program from console, you need to set the classpath to your bin or target directory and to all libraries ('jar') which are referenced by your project. After setting the classpath correctly you can start your program with java <qualified classname>. Starting the program this way does not use any of Maven's functionallity yet at all.
Have a look here to use Maven https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
Maven will create a library jar which contains all classes of your project.
You can set the classpath to the generated library and start your programm using java <qualified classname>
EDIT due to comment
Here is an example for setting the classpath using the Windows OS
console. You can put this line also into an appropriate Windows batch (.bat)
file and start your program,
set classpath = .;log4j.jar;lib/any-other-lib.jar
java org.<whatever>.MyProgram
for further information for setting the classpath on other OS's you may also have a look at
setting Java Classpath in linux?
I am writing my first Spring application with Spring 4 groovy configuration. I am using Eclipse IDE. As usual I have all my Java files (actual domain/services etc.) in src/main/java and all my Spring Groovy configs in src/config/groovy.
Both are added as source to the classpath in Eclipse. As a result Eclipse compiles my groovy files into .class files and copies into target folder. But the problem is I am trying to load my Spring groovy classes from classpath as below, but it doesn't work as JVM can't find .groovy files as Eclipse compiled them into .class files.
def ctx = new GenericGroovyApplicationContext()
ctx.load("classpath:spring/SpringConfig.groovy")
Is there any way to force Eclipse not to compile my Spring groovy files but copy them to the output folder like xml files? For a test application I can probably use the file system path like "file:..." but I don't think I want to use it in PROD, so I am trying to write something that works both in IDE and PROD. Any suggestions? Thanks.
Ok, I should have looked around a bit hard! Resolved it myself.
In Eclipse, Go to Window->Preferences->Groovy->Compiler
Just check the option "Enable script folder support" and the pattern to cover the path where your groovy scripts are, then eclipse just copies them to the output folder like any other scripts.
I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.
I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.
I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:
Exception in thread "main" java.lang.NoClassDefFoundError: ...
Any help would be appreciated.
It must be a CLASSPATH issue.
The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.
This is likely a classpath issue as others have said.
One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;
Extract classes into jar
Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
Place jars in a subdirectory next to the jar.
Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.
Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.
Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).
I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.