How to run a external .class file in eclipse? - java

I am taking a course on Coursera and as part of a quiz I am supposed to download their xyz.class and run it. I want to do it in eclipse as all of jar files are added in eclipse and I dont want to modify the class path of system as it is very untidy.
But I dont know how to run a external .class file through?Can anyone help me with that?

Why do you want to run it in Eclipse ?
java classname
souhld do the trick. You can't use Eclipse to run an external class file with a main method without creatin a java project with this class in its class path.

Right-click your Eclipse project, Build Path, Configure Build Path, Libraries, Add (External?) Class Folder.

Just run
java xyz
eclipse might not required.
If you want to really work out through the eclipse then refer How do I include .class files in my project in Eclipse? (Java) answer.
There is another way of approach without using eclipse.
Approach is by creating a executable jar file and by including all required libraries & xyz.class.

Right click on the class file from the Package Explorer -> Run.

goto file >> new project >> Import>> add executable jar files >> select your path where you have stored your .class files>> check include in work place
apply>> ok >>>>>>>>>>>>>>>>>>>>Done

Related

How to make Java package with classes

I would like to make a Java package in a JAR file with precompiled classes such that other Java projects can consume these. How do I do that? It seems to me that most guides I have found expects a Main class/method to be available, but I do not want this to be an application that runs by itself. Furthermore, the resources (various files) inside of my project should be put into the JAR, since my app depends on these. Is this possible? I am (by the way) using Gradle.
A claim has been made that this question is a duplicate of this: Java creating .jar file. However, this question assumes the existence of main methods, and it does not concern how to include resources.
You can create the jar from the command prompt.
Copy all the classes that you want to include into a folder.
Then open that folder in command prompt and issue this command.
jar cfv YourProjectName.jar *
And a JAR will be created in the same folder containing all the classes.
Another solution:
If you are using eclipse try:
Right Click on the Package -> Export -> java -> jar file
You could also select the Classes and right click on them instead of the Package.
Edit:
Refer to https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for more details on this command.
you can put all your methods/functions in class file then export it to .jar
then add the jar to your project's build path. Now you should be able to call those functions from your current main java class.

creating .jar file with a file that calls a .jar file

Sorry if this is a little confusing. I have a java file that uses a compilation command with another jar that I want to make into an executable(not .exe). I followed an online example to make a regular jar but get the NoClassDefFoundError due to the jar that it needs to run not being included. I am not sure what I either need to add to my manifest or the jar creating command to achieve this.
Thanks
I have not written java in a long time but try this,
Export(right click and select it from options) the project you're working on
Select runnable jar file.
Decide what you want to do with libraries
Finish.
NB: Try having the class files in the same location as the jar

Exporting processing to a jar file

I've been working on a processing application using ControlP5 and Twitter4j. I want to have my project run from a single jar file from any operating system. Basically I want to package up my application. My application uses images. I've been browsing for more than an hour, but I cant find how to do this. Any suggestions?
using
processing 2
twitter4j3
Thanks in advance!
I dont know if you can directly do it from the Processing IDE however, if export your sketch to a Java applet then locate the .java the the sketch folder you can use this in conjunction with Eclipse to export to a jar file.
So, I know that this post is very old but if you are still looking for a solution, or to other people that see this thread, it's relatively simple.
Export the project
In the folder with the exported project (something like application.windows64), navigate to lib and find core.jar and project name.jar (you need to have file name extensions visible)
Rename the files to .zip files
Extract core.jar to whatever folder
Extract project name.jar into the same folder (make sure you don't do it into a subfolder)
Click yes if it asks if it wants you to replace a file (if it doesn't you extracted the files incorrectly)
Delete core.jar and project name.jar
If the project uses images, move them into the same folder as all the other files
Select all of the files in the folder, right click, hover over send to and select compressed (zipped) folder
Rename the .zip file to name of project.jar
This might be old, but i still find other posts about it on processing forums.
This is the best way to run processing project as a jar file.
When exporting application, you will always end up with a lib folder inside exported application(whether for Linux and Windows). For windows, open command prompt(or power shell), you can use right-click+shift and then click on open power shell here.
After that you can run the following command.
java -classpath lib\* DisplayDepthStream
Now DisplayDepthStream is the name of sketch file.
To explain the command, -classpath lib\* tells java to add everything under lib directory to the class path. And DisplayDepthStream is the name of my main class.
Hope this helps~!
Chears

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.

how to update a external jar with my java class file on eclipse

In my java project, i have added some external jars. I want to update a particular jar with my java class file. Please help me how to do this.
Regards
Rajasekaran.G
If the jar you want to add your class to is a 3r party library, I would recommend against doing what you described (it can lead to subtle surprises and unintended side effects).
If the jar you want to update is your own, I would mode the source for the class into the project that generates that jar and it should be added automatically during the generation of the jar.
If you really want to do it by hand, you can unjar the external jar, add your .class file to the resulting directory structure and jar it up again.
You need to update the jar independently try this,
jar -uvf <existingjar> <class file in appropriate folder structure as per package>
e.g.
jar -uvf current.jar NewClass.class
or if your class is in some package say com.my.util, place your class file in com/my/util/NewClass.class and execute the following from prompt
jar -uvf current.jar com
This will update the current.jar file.
If its an external jar, i wonder if you can even do this? If its your own jar, follow the option given by Attila
try to open your .jar file winrar and place your java class wherever you want on the jar.

Categories