Compiling JAVA class file within TomCat Server - java

I have a class file that creates a monthly report by talking with a MySQL database, it also creates graphs as JPEGs (Hence why it must be in the TomCat Directory and not compiled as an app)
The front end simply uses this data to display a "Monthly Report".
My problem is I cannot figure out how to run the class file once it's compiled from the server. The Class file works 100% in eclipse and then automatically publishes to a server (locally).
I'm trying to compile the JAVA class file and run it locally on a Windows machine, the live version is on a Linux environment.
Here's what I have so far:
So compiling the class and running it: (Tried with -cp as well)
java C:\p_sys\tomcat\webapps\PR\WEB-INF\classes\au\gov\vic\pdd\PPA createreportsmonthly
We get the following error:
C:\p_sys\tomcat\webapps\PR\WEB-INF\classes\au\gov\vic\pdd\PPA\createreportsmonthly>java CreateMonthlyReportTable
Exception in thread "main" java.lang.NoClassDefFoundError: CreateMonthlyReportTa
ble (wrong name: au/gov/vic/ppd/PPA/createreportsmonthly/CreateMonthlyReportT
able)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can anyone help me out here?
* EDIT
Some clarity
CreateMonthlyReportTable.class is located within:
C:\p_sys\tomcat\webapps\PR\WEB-INF\classes\au\gov\vic\pdd\PPA\

have you tried
java -cp C:/p_sys/tomcat/webapps/PR/WEB-INF/classes<space> au.gov.vic.pdd.PPA.CreateMonthlyReportTable
should be a space character

Related

Can't seem to get my Java to work in VS Code

I have been trying to get VS Code to work and compile my Java code. I have finally given up trying to fix it myself.
I can't compile my code, I can't run it. I don't even know what's wrong with it. I really want to use VS Code because it's so clean and versatile (so please don't recommend me other IDEs).
Here is my code:
class DataStruct {
Run|Debug
public static void main(String[] args) {
System.out.println("Hello");
}
}
(Manually transcribed from this image.)
Here are my system variables:
This is what I get:
PS C:\Users\SvenH\AppData\Roaming\Code\User\java-ws> cd "c:\Users\SvenH\AppData\Roaming\Code\User\java-ws\" ; if ($?) { javac DataStruct.java } ; if ($?) { java DataStruct }
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: DataStruct has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
PS C:\Users\SvenH\AppData\Roaming\Code\User\java-ws>
Make sure you are compiling and using the same java version.
One simple way to do it is:
uninstall JDK and JRE
download JDK and JRE, both at the same version
install them

Running java file in cmd - error given

I recently moved a project from one machine to another but I am now given an error when attempting to run java files on the new machine. I'm trying to run a java file from part of an Android project in command prompt but I am given an error. The file compiles okay but fails to run. Here is the error I am given;
Exception in thread "main" java.lang.NoClassDefFoundError: ChatServer (
e: edu/UTEP/android/ChatServer)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Any ideas why I am receiving this error?
Your ChatServer is having a package declaration of edu.UTEP.android in the top. So either you need to remove that package declaration recompile your java file again and run it
(or)
you need to create folder structure like this
CurrentDir/edu/UTEP/android/
Keep your java file in android folder and invoke java ChatServer from CurrentDir. Any of these two will solve the issue :-)
For more info on packages, you can refer to my previous answer on a similar issue here
Make sure that the .jar that provides ChatServer is in your runtime Classpath.
This issue rise when jvm don't file class path.
check where u have placed your jar file or lib folder.
Move to that directory then run javac and then java .
Issue will be resolved.

Exception when running a Jar on another computer (other Java version)

Playing with a little network program, created a Jar with netbeans. Runs just fine on my computer java 1.7.0.60, but when I take it to another computer that has a newer version of java 1.7.0_65 it will not run, I get this error:
C:\Program Files (x86)\Java\jre7\bin>java -jar ReverseEchoServer.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: reverseechose
rver/SocketClient : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
I have read several posts on here that talk about PATH, have looked at that but no luck. I belive its the version differance. Any advice would be most appreciated, Thanks!
Java class files compiled for Java 7 have Major/Minor versions 51/0. If you have a JVM implementation that wasn't prepared for Java 7, it would give you this error indicating "I don't know how to interpret and run this class file."
Try either compiling for an older JVM version, or upgrade your JVM.
Use the Target and specify the version in which it needs to be executed using target switch followed by version number.
javac -target 1.7 yourJarName.jar

Why do I get "A Java Exception has occurred" after I export my application to Jar?

Well, I exported my application into JAR with eclipse, but now whenever I try running it I get the: "A Java Exception has occurred". When I try to run it through command line it says:
C:\Users\Arturas>java -jar D:\Dropbox\EclipseWorkspace\Exports\Dantracio_skaiciu
oklis3.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widge
ts/Composite
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Composite
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
C:\Users\Arturas>
Any solutions to this? I just hate java for this... It never works on desktops when you want it to... sorry I'm really stressed out from this, I'm overreacting...
Any ideas what could cause this? I doesn't work on other computers as well. Right now it's using Java 1.7. And the application runs perfectly on Eclipse.
You need to get your dependencies. Looks like you are depending on swt, which is not part of the core java language. You'll need to get all the third party jars that you depend on and include them in your classpath. It runs perfectly on eclipse because eclipse takes care of setting up the classpath for you.
You can set the classpath in your manifest file as described in the documentation, or you could include a script to start your application that will use the -cp command when running the jar, or you can use something that will package everything into one monster jar like the solutions to this question.

Why am I getting an exception when running a JAR file from the command prompt?

Hi I have made a runnable JAR file useing export option on eclipse. However, when I go to run the file via command prompt i get the following
Exception in thread "main" java.lang.NoSuchMethodError
at org.eclipse.jdt.internal.jarinjarloader.RsrcURLConnection.getInputStream(RsrcURLConnection.java:43)
at java.net.URL.openStream(Unknown Source)
at sun.misc.URLClassPath$Loader.getResource(Unknown Source)
at sun.misc.URLClassPath.getResource(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Does any one have any ideas as to what im doing wrong? pretty new to the whole creating runnable files etc.
Your jar is compiled with a newer version of java than the computer you are running it on.
Extract the jar file look for the file META-INF/MANIFEST-MF. This file should have an entry
Main-Class : <you fully qualified classname having main method>
Check if the class you mentioned as Main-Class has a main method (public static void main(String[] arg))

Categories

Resources