Compile and run Groovy on Linux server - java

I'm trying to move my Groovy project to a server with no Eclipse. In order to minimize the uncertainty I compiled my project in Eclipse (on my PC) and copied the project(with all .class compiled in /bin) to the server in a whole. After that I kept meeting the error:
Error: Could not find or load main class edu.xxx.textrank.TrendAnalysis
This is how I ran my code:
All .groovy and .java are in folder .../TextRank/src/edu/xxx/textrank/, all compiled .class files are in folder .../TextRank/bin/edu/xxx/textrank/, and all dependent .jar files are in folder .../TextRank/lib/. In directory .../TextRank/bin I ran java -cp ../lib/*.jar:. edu/xxx/textrank/KeywordExtractor and met the error. TrendAnalysis.class was compiled from TrendAnalysis.groovy and there are also some auxillary .class compiled from closures in groovy:
TrendAnalysis$_allPeriodKeywordStudy_closure1.class TrendAnalysis$_certainPeriodKeywordStudy_closure3.class
TrendAnalysis$_allPeriodKeywordStudy_closure2.class
However, things are not quite same for those .class files compiled from .java. For example I was able to run Patterns.class which was compiled from Patterns.java without inner classes by the same command
[xuch#xxxxx bin]$ java -cp ../lib/*.jar:. edu/xxx/textrank/Patterns
abcdefghijk
I'm wondering what could be the real culprit. Can I just run the .class files without re-compiling them on another server? Or am I able to just run the main .class without calling all the innerclass files (auto-generated by Groovy)?
Any suggestion and help is appreciated! Also quick solutions will help (like how to compile all things into an executable .jar etc). Thanks a lot!!
Edit: One important thing to mention, all my .java and .groovy are in package edu.xxx.textrank. In other words they all have package edu.xxx.textrank;

Related

Java can't compile in terminal because of external libraries

I'm on Ubuntu 22.04 trying to run my code in the terminal.
The program works without problems in VScode, also when running multiple instances.
The program is consisted of six class files.
The trouble occurs when I try and run it with terminal. When compiling the java file
with javac it shows errors at places where I use the external libraries.
If I compile it with VScode and run the class file in terminal, I get the following error java.lang.ClassNotFoundException
This is causing me problems since I'm also supposed to dockerize the program.
You can add the following code in your setting.json file "java.project.outputPath": "bin",
This will be the .class file generated by VS Code in the bin folder of the same directory when running the Java code.
You can use the java command after entering the file directory with the cd command.
This generally indicates that the class path with which you're compiling your program does not include the correct paths to your libraries. Assuming your libraries are jar files, your javac command should look something like this:
javac -cp libs/lib1.jar:libs/lib2.jar srcs/*.java
where libs/ is the relative path to your libraries and srcs/ is the relative path to your own java files.
And when you run the program, make sure your class path includes both the locations of your libraries and the location of your class files (which in this case would be the current directory):
java -cp .:libs/lib1.jar:libs/lib2.jar <MainClass>

Class files (.java) inaccessable from Jar

I created a Jar file.
When I use it in my project it seems to be able to find the Package name just fine but I can't use the classes within.
Package name, but no classes after.
Did I do something wrong when creating the Jar?
I ran "jar cvf Adapter.jar *".
Any suggestions would be a great help!
A Java JAR file is supposed to contain compiled .class files, not .java source files.
You'll need to compile your files using javac and then use jar on the output.
As NimrodArgov pointed out, your IDE (apparently) can't find the class because it does not exist as compiled .class file as it would be expected. The JAR package only contains the Java source files.
As far as I can see, there is no need for you to pack your classes into a .jar file either. Just make sure the containing folder structure is in your Java Classpath.

how to extract and update class file from a jar?

this is what i need to do:
1) extract files from a .jar archive
2) take two of them, decompile them, make some updates and then re-compile them
3) make the jar archive again
Basically the problem is that after my updates i cannot compile the class anymore because i ve got a lot of errors about dependencies by the javac compilator.
i'm extracting files from .jar using archive manager of linux
i'm decompiling classes using java decompiler online
i'm trying to compile the file again using the command javac
thanks please help me

Batch - Natives, Jars, and loose class files

I am running Windows 7 64-bit.
I would like to make a .bat file that will run my eclipse project's main.class using (preferably) only that .bat file. The project is still undergoing construction so exporting a runnable jar every few minutes isn't an acceptable solution (nor is installing eclipse on all machines). Furthermore I don't really want to install any more plug-ins, I know there is a way to do this with a .bat but haven't been able to figure it out. Here is what the project root folder looks like:
C:/.../ShootEmUp <-inside there we have:
bin/
lib/
natives/
lots of .jar files
res/
src/
ShootEm/
OtherPackages/
ShootEmMain.java
.classpath
.project
RunMe.bat
I have tried more combinations inside the .bat file than I care to admit. Right now RunMe.bat looks like:
#ECHO OFF
javac src.ShootEm.ShootEmMain.java
java -cp lib/*.jar;. src/ShootEm/ShootEmMain;
PAUSE
This currently produces a javac: file not found: src.ShootEm.ShootEmMain.java error
I have also tried putting the .bat inside the folder ShootEmUp/src/ShootEm/ (next to the class with public static void main [ShootEmMain.java]) and editing the RunMe.bat code to:
#ECHO OFF
javac ShootEmMain.java
java -cp ../../lib/*.jar;. ShootEmMain;
PAUSE
This seemed to work a little better as the error I would get then was:
ShootEmMain.java:5: error: package ... does not exist. (the a whole bunch more for all the other jars in the lib folder)
It's useful to note I've already added C:\Program Files\Java\jdk1.7.0_25\bin; to the front of the value of the Path environment variable.
I have also tried classpath instead of cp and a whole crazy list of syntax combinations for each (once I was well past frustrated) to no avail.
Also I realize my .bat example doesn't set the natives as I'm not really sure where that fits into this mess. Thank you in advance!
You should look at using a tool like ant or maven to build your application. This will streamline producing your artifact.
If it's unreasonable to export a working jar across your environment, I boggle at how you could imagine it's more manageable to export your source code around your environment.

java.lang.NoClassDefFoundError. Class not found during runtime.

I am working on eclipse, and I have the need to use external library's. For example Jsoup and JXL.
Now what I have done so far is: First created a "lib" folder in my project folder. Afterwards in eclipse, click on project properties, Libraries tab, add external jar and added the jar in the lib folder.
So this solve my compilation issue. Now, when I run the program (I go to project/bin and in the console execute: java ProgramName ; I get
java.lang.NoClassDefFoundError:
Now to testing, I added the Jar file to the folder where Main.java is and Now, I have been able to run the program doing the following:
javac -classpath ./path/to/jar Main.java
java -classpath ./path/to/jar:. Main
And this works.
So the first thing that comes to mind is that I have to tell java where to find the respective libraries. If this is correct? How do I do it?
java -cp ???(dont know what to put here)
But moreover. I have another issue. I am writing this program in a computer, but I am going to use it in other which probably don't have those libraries. How do I solve this issue?
I like to use something like the following:
java -cp myjar.jar;lib/*.jar com.foo.bar.MyClass
This adds not only my jar to the classpath but those in the lib directory as well.
If you want to run your jar on another computer, you will need those jars as well, you cant just have your jar. Why not just also package your lib directory along with it?
To get your program to run you have two paths to worry about
The path to the jar files that are your applications dependencies (like jsoup.jar) (lets call this lib)
The path to the directory containing the classes of your app (lets call this classes)
The general form of the command line you need is:
java -cp lib/jsoup.jar:classes Main
If you have more libs
java -cp lib/jsoup.jar:lib/jxl.jar:classes Main
A general note on packaging your app for release to other computers. You might want to consider making a jar of your own app, probably best done using http://ant.apache.org/manual/Tasks/jar.html
Another option is to produce a "one jar", which makes one large jar, bundling in all the classes you need from your libs and all the classes in your app. You can then make the jar executable for a nice out of the box solution. Have a look at http://one-jar.sourceforge.net/ and https://code.google.com/p/jarjar/
if you have this structure:
project folder
... code
... libs
then from the code folder:
javac -cp .;../libs/*.jar yourmainclass.java
java -cp .;../libs/*.jar yourmainclass
When you need to compile and run this project, take all the folder and do the same in other machine.

Categories