run the spring project from command line - spring

I have created one spring project in STS (spring source tool) which works (compile and execution) fine in STS, but when I am run the class file from terminal I am getting an error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: com/XXXX/XXXX/Operations
Caused by: java.lang.ClassNotFoundException: com.XXXX.XXXX.Operations
For reference below is my .classpath file:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java" />
<classpathentry kind="src" output="target/test-classes"path="src/test/java" />
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/
org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5" />
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
<classpathentry kind="output" path="target/classes" />
<classpathentry exported="true" kind="var" path="M2_REPO" />
</classpath>

Your error suggests that the "Operations.class" file is not in the target/classes folder (or you have a typo in your package name).
First ensure your class is where you expect it:
cd [project root]/target/classes
ls com/XXXX/XXXX/Operations.class
If this proves the file is present, you should be able to use the java command you mentioned:
java com.XXXX.XXXX.Operations
Make sure you do not add ".class" at the end, as Java will assume you are trying to load a file called "class" in the "Operations" package.
If the file is missing (I expect it will be), you will need to execute a build in STS (Project -> Build Project) to create it.
Update
You mention in the comments that you are getting the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
This is a very different problem, it means you have not referenced the Spring libraries in your java command, so Java doesn't know where to find them.
I recommend you package the program with STS before attempting to run it. This is the typical way Java developers build and deploy applications. This article provides a simple tutorial on how to do this (STS is a variant of Eclipse).
Addition - your updated question suggests you are also using Maven in this application, in which case you can use "mvn package" to build the project instead of Eclipse. Read this tutorial to get started on using Maven: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
If you REALLY want to run the program without packaging it; you will need to reference ALL the Jar files you are using in the command (this is only an example, replace "spring.jar" with the correct Jar files you are using):
java -classpath .;../../lib/spring.jar com.XXXX.XXXX.Operations

Usually, people creates jar-file, which contains all classes and resources need to start the application. After that, its easy to start the app:
java -cp youapp-full.jar com.XXXX.XXXX.Operations
And Operations class must have main method ofcourse

Related

What, in Eclipse, modifies a project's .classpath to add the attribute exported="true" to classpathentry?

Looking at a .classpath file of a project that I inherited, I found the following difference between the committed file (to Git) and the working directory:
- <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
+ <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
If I am to revert that via the Eclipse GUI, what in the project's properties do I need to change?
Found it!
The exported="true" or exported="false" in the .classpath file is determined by checking/unchecking the checkboxes that are accessible by Right-click project > Build Path > Configure Build Path... > Order and Export [tab]

.java is not on the classpath of project, only syntax errors are reported

I could not find a proper answer to this error in other posts.
I am using Maven for building my project in VS Code and whenever I create a class this error appears.
Do I need to do anything with .classpath file?
Correct me if I am mistaken, this error is pointing out that the class is not recognized by the .classpath file in which it belongs to, so I have to address it in that file.
Tnx
It seems like no one else knew it!
The problem was regarding "folder convention" and "maven-dependency-plugin".
As I created my Maven project with the line below via windows command line, maven did not install dependency-plugin and that created the first problem.
Second, what I meant by "folder convention" was, deleting "java" folder which is created by executing bellow Maven command line, caused me a problem since Maven compiler does not find any Java project to go to and execute codes. Deleting Java folder will also lead to the error above because no Java file is recognized, so no java files will be added to .classpath folder respectively.
mvn archetype:generate -DgroupId=name -DartifactId=name -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
I had the same problem.
From Welcome screen, go to Student-->Getting Started-->FAQ-->Find the "Why do I see tons of problems and red squiggles?" portion and click " Clean Workspace". That fixed my problem.
I have the same problem.
Just close the vscode and open it again.
Hope it works
Since i was using a different structure from what the maven project expected i had to manually change the .classpath file from pointing to the wrong folder. Only after this that my imports started to be correctly resolved.
Original:
<classpath>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
...
Modified path attribute:
<classpath>
<classpathentry kind="src" output="target/test-classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
...
I had the same problem and it was just about folder structure of vscode. when you created java file, it would have created a .java file (and maybe a .class file). Just simply move the .java file into src folder and .class file into bin folder and the problem is solved.
link of screenshot if you need
i have noticed when i open multiple cmd to type "code . " this error occurs
try to close all CMD's and reopen project i think it will go away
Simply create your files inside the src folder your problem will be solved.
" If you already created them outside the src folder move them into src folder"

Unable to Create .go File Within IntelliJ IDEA

I'm using Intellij IDEA Community 2016.3 with the golang plugin.
Recently within Intellij I created a new package then I tried to create a new go file in that package. Intellij gave me a template error message, something like this:
Unable to parse template "Class" Error message: This template did not produce a Java class or an interface - during New -> Java Class
(To create the file I right-clicked the package and selected New > Go File.)
I then went into the file system and manually created the file. After a brief pause, Intellij closed the pane on the left that lists all my packages. Intellij also corrupted the project such that I was not able to view the project pane again.
I recreated the project and everything seems to be fine now but I don't want to have to do all that again each time I create a .go file.
I don't know if this makes a difference, but the package I tried creating the file in is a download from github. Intellij has been telling me this:
The directory C:\go-workspace\src\gopl.io is under Git, but is not registered in the Settings.
It's someone else's project -- files for learning go-- and I don't plan on sending any pull requests. I simply created the package and file there because they are part of learning go.
How do I create .go files in Intellij?
Added 21-Jan:
Here are my project settings:
Make sure you got the Go setup properly before continuing.
Next I'd also make sure the plugin setup is good(maybe reinstall after making sure your go setup is ok), such as the project SDK is set to something Go and not Java as that's usually the default(see image below) and IntelliJ can detect your GOPATH.
If the project your'e working on was not an IntelliJ module before and you just opened it, make sure the .iml file is a Golang module (IntelliJ usually asks you to do this automatically if it's not already done, but you need to setup the project SDK to Go first I assume)
ie.(my hello.iml);
<?xml version="1.0" encoding="UTF-8"?>
<module type="GO_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="GOPATH <hello>" level="project" />
</component>
</module>
Here is a comprehensive step by step guide for a starter setup;
https://rootpd.com/2016/02/04/setting-up-intellij-idea-for-your-first-golang-project/

When causing a makefile to change directory and run ant in that new directory error "JAVA_HOME is not defined correctly"

Even if you have Java and/or ant installed through the windows installers, you still need to set your environment variables. Make sure you are linking your java to a JDK rather than a JRE, my guess as a newbie is that when you are compiling your own code you should always use the JDK as it has all the tools you need.
Original post:
This is all happening on Windows. I call make. It checks out a "code" folder from svn into the directory that makefile is in. Inside "code", is a build.xml. I want my makefile to change to that directory and then run ant on it. I am getting the error in my command prompt:
C:\Users\Ryan\Desktop\maketest>make
svn co (address removed)
Checked out revision 107.
cd code; ant clean compile jar run
uname: not found
basename: not found
dirname: not found
which: not found
Error: JAVA_HOME is not defined correctly.
We cannot execute java
make: *** [runant] Error 1
I checked here http://sunsite.ualberta.ca/Documentation/Gnu/make-3.79/html_chapter/make_5.html#SEC46 and it seems that this should be do-able in my makefile:
runant:
cd code; ant clean compile jar run
as this will open the command in a subshell but it will also run ant in that subshell.
This all works fine if I manually change into the code folder and run ant. I only get this JAVA_HOME not defined correctly error when I try to do this all from a single makefile.
makefile:
commands = checkoutcode checkoutdocs runant
svnCode = https://version-control.adelaide.edu.au/svn/SEPADL15S2UG7/code/
svnDocs = https://version-control.adelaide.edu.au/svn/SEPADL15S2UG7/updateDocs
ifdef version
REVISION = -r$(version)
endif
all: full
full: checkoutcode checkoutdocs runant
# ensure you use a single tab for the commands being run under that name
checkoutcode:
svn co $(svnCode) $(REVISION)
checkoutdocs:
svn co $(svnDocs) $(REVISION)
#change directory into the code folder and compile the code
runant:
cd code; ant clean compile jar run
build.xml:
<project>
<!-- add some property names to be referenced later. first one is lib folder to hold all libraries -->
<property name="lib.dir" value="lib"/>
<!-- images directory used to compile with the images and also add them to the jar -->
<property name="src/images.dir" value="images"/>
<!-- ensure the classpath can see all classes in the lib folder by adding **/*.jar -->
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<!-- "Target" attribute is what we will write as a parameter to ant in the command prompt or terminal -->
<!-- eg. "ant clean" will run the commands inside the target tags, which is just to delete the directory
"build" -->
<target name="clean">
<delete dir="build"/>
<echo message="Cleaned "/>
</target>
<!-- compile command. creates a directory for the classes to be stored in, instead of the root folder
and then compiles everything in the src folder, using the classpath properties we set earlier -->
<target name="compile">
<mkdir dir="build/classes"/>
<javac includeantruntime="false" srcdir="src" destdir="build/classes" classpathref="classpath"/>
<echo message="Compiled "/>
</target>
<!-- jar command. this creates an executable jar file called Robot.jar which can run the program in full.
it uses the images and it uses the lejos libraries and embeds them into the jar -->
<target name="jar">
<jar destfile="Robot.jar" basedir="build/classes">
<fileset dir="src/images" />
<manifest>
<attribute name="Main-Class" value="Main"/>
</manifest>
<!-- this line adds everything in the lib folder to the classes being added to the jar,
ie. the lejos classes ev3 and pc -->
<zipgroupfileset dir="${lib.dir}" includes="**/*.jar" />
</jar>
<echo message="Created JAR "/>
</target>
<!-- run command. runs the program. Running the program in the command prompt or terminal is good
because it allows you to see any exceptions as they happen, rather than hiding them in eclipse -->
<target name="run">
<java jar="Robot.jar" fork="true"/>
<echo message="Run "/>
</target>
</project>
I am required to run this on different machines without any extra configuration performed, and will not be able to confirm the location of Java on those machines - the only guarantee I will get is that Java is installed and the environment variables work.
Just answering to allow you to mark as solved.
Even if you have installed Java with the oracle installer, you still need to create environements variables.
Even if you have Java and/or ant installed through the windows installers, you still need to set your environment variables. Make sure you are linking your java to a JDK rather than a JRE, my guess as a newbie is that when you are compiling your own code you should always use the JDK as it has all the tools you need.

javah cannot find dependent classes from spring-context-3.0.3.jar file

Am usning ANT tasks to compile java classes and then generate .h files using
javah [ version "1.7.0_55" ] on windows 7
following is the code snippet from my build.xml
...
<property name="build.dir" value="./main/test_target"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="external.lib.dir" value="./externalJars"/> <!-- contains spring-context-3.0.3.jar file -->
<property name="jni.output.dir" value="${build.dir}/jniHeaders"/>
..
...
<target name="compile-header" depends="build">
<exec executable="javah">
<arg line="-classpath ${classes.dir};${external.lib.dir} -o ${jni.output.dir}/MyNativeImpl.h -verbose -force examples.MyNativeImpl"/>
</exec>
</target>
...
..
But I am not able to generate the .h files and get the following ERROR
Error: Class org.springframework.context.MessageSource could not be found.
even though I have added "spring-context-3.0.3.jar" to externalJars dir => ${external.lib.dir}
My java file MyNativeImpl.java uses package "import org.springframework.context.MessageSource; "
Java file compilation goes fine using ${external.lib.dir} and MyNativeImpl.class also generated under ${classes.dir}
Not sure what am I doing wrong!!
Almost spend 1 day searching on SS but could find a specific solution.
I have many such dependent jars from spring framework to compile my app fully under ${external.lib.dir} hence wanted to know how could this be resolved so than JAVAH could find classes within .jar files !!
Surprisingly I have a work-around which is:
1. unzip the contents of pring-context-3.0.3.jar to CLASSES DIR ${classes.dir}
2. now my ${classes.dir} has following contents
a. examples/MyNativeImpl.class
b. org/springframework/context/MessageSource.class ( and other classes )
3. Now JAVAH doesn't complain and generate my MyNativeImpl.h
But I am now sure why it doesn't find the same MessageSource.class when I use spring-context-3.0.3.jar ??
Is there something I am missing or this is the only way :-( any help is highly appreciated.
Thanks in Advance
Regards,
Vivek
When specifying jars on a classpath, you must list each jar. The directory containing the jar is not enough, that only works for classes, explaining why your work-around works.
I suggest something like:
<path id="javah.path">
<pathelement location="${classes.dir}"/>
<fileset dir="${external.lib.dir}" includes="*.jar"/>
</path>
<pathconvert property="javah.classpath" refid="javah.path"/>
<exec executable="javah">
<arg line="-classpath ${javah.classpath} -o ... />
</exec>
PS
Why don't you use the javah ANT task?

Resources