Running old Java Programs - java

a few years back in college I wrote TONS of simple little java programs... I haven't used them in a while and I would like to go back and revist them. I believe I used Netbeans as my IDE (but i also could have used Eclipse... cant remember)..
anyways long story short.... I cant remember how to compile them and I cant open these as projects in any of my IDEs....
I've tried using javac and then java to run a few in the command line, however one specifically will not run and gives me this error:
Exception in thread "main" java.lang.NoSuchMethodError: main
I know this is because I don't have a public static void main method in this program. I can't remember why I don't.... but here's in theory how it should work.... I have 3 program files:
race.java
bicyclePanel.java
controlPanel.java
race extends JApplet and has a method public void init().... which creates an instance of controlPanel class.... controlPanel class creates an instance of bicyclePanel and so on and so forth....
my question is.. am I supposed to have a main method? How can I rebuild this into an IDE and run? It's been so many years I cant remember all the in's & outs of java :(

Yes, you are supposed to have the main method if you plan on running it as a standalone Java application. In Eclipse you can use the Applet runner to run your applets directy as well. It's Run As -> Java Applet.

Fire up NetBeans or Eclipse and Import the Projects with existing sources. It will do most of the stuff. You may have to adjust the broken references.

Related

NanoHTTPD: How do I add it to a current Java eclipse project and use it?

This is my first time with Java and Eclipse. I started a brand new Java project and I want to import/add NanoHTTPD into it. How do you this?
This is NanoHTTPD's site: http://nanohttpd.com
Thanks!
Edit
Lesson learned, be specific or you get backslashed for asking. I edited the question and here's some background and the problem I'm running into.
I'm developing a Nodejs backend that needs to query a JAVA project I was given. Pipes are a no go because the services will run on different machines. Tomcat seems like an overkill so I decided to use NanoHTTPD to develop the web service. I come from Ruby & Nodejs so compilation and Eclipse are very new to me. First off, I have no JAR file just TAR and ZIP and from what I read they are fundamentally different. However, I tried importing the TAR and ZIP files as recommended but the structure I get in Eclipse does not seem right compared to the JRE System Library or others I've seen. Notwithstanding, I went ahead and tried to import the package from my Main.java file
package fi.iki.elonen;
public class Main {
public static void main (String[] args)
{
System.out.println("Main");
}
}
When I try to run it I get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at Main.main(Main.java:4)
I found a great article from IBM "Managing the Java classpath (UNIX and Mac OS X)" where it mentions that an IDE such as Eclipse can alleviate the pain of dealing with the source path, classpath and compilation. Unfortunately, I'm afraid this is where I might be getting stuck.
I tried uploading images of what I have but apparently I'm not popular enough yet to do it.
Could someone help me figuring out how to not only import libraries but using them on projects? Even just a URL to a clear Linux/Mac OS X post that explains import with multiple packages would be great.
NanoHTTPD is designed to be very lightweight.
I just cut and pasted the 'NanoHTTPD' class from the source on github, its all in there - and pasted it as a class into my own project.
Then I created a subclass of nanoHTTPD, overrode the 'serve' method to send my own stuff and it was job done.
Download the jar, drag it into the project, and right-click it to add it to the build path.

How to run my Java code that I created with sublime editor?

I'm learning Java in school and they told us to use eclipse since it provide a whole IDE, but somehow I like working with sublime better, I don't know, maybe because its cleaner and feels really light weight compare to eclipse.
Now, if I created a Rectangle.java class and I have also created a Program.java class which includes the main:
public class Program {
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle myRect = new Rectangle(double w, double h);
myRect.show;
}
}
So where do I run this..? in the terminal? and how?
Of course I will not have a debugger unfortunately, but please help me to figure out how can I use sublime while learning Java, I like this editor and want to use it.
Thanks guys!
Sublime is just a text editor. It is a nice one with syntax highlighting and other nifty tools but it is not a compiler. IDEs will take care of the complilation step for you but since you are opting to use SublimeText you will have to compile the Java files into their bytecode manually.
You will need to use javac for this. You can find it inside your JDK. Where it is located and how you call it depends on your OS and how you installed the JDK.
The most basic use would be to compile a single Java file without any dependencies (run from the command line inside the folder containing your source code):
/path/to/javac YourFile.java
That will output a YourFile.class file beside your .java version that can be run by your JRE with (again from the folder containing the *.java and *.class files):
java YourFile
For more complicated uses such as including 3rd party libraries, class paths and cross class dependencies here is an example tutorial (many of which can be found with a simple Google)
The easiest way is to compile and run it from terminal. Since you call it terminal, i assume you are working on a mac. This has been answered before.
I have written about compile and run a Java program with single key Binding in my Blog at this link https://sainirahul.wordpress.com/2015/07/15/pure-sublime-coding-and-running-java-program-using-sublime-text-editor/
Sublime Text has a feature called Build System. It basically means that program will be executed inside a console in Sublime window after pressing Ctrl+B. Unfortunately this inner console doesn't support input (as I recall).
Helpful links:
https://gist.github.com/jfcalvo/3789664
https://www.sublimetext.com/forum/viewtopic.php?f=3&t=4805

Issue running java program from batch file, runs fine in IDE

I'm doing some basic java homework for a class on my new laptop - issue is, I can't seem to get the program to compile and run from my batch file using the directions the instructor gave me.
I've set the Path variable to my JDK inside the Environment Variables settings.
My program is a simple shipping program to keep track of shipment information - I have the program working flawlessly in NetBeans (which our instructor advised us to use for developing the code), but he's going to be testing them using batch files, so we're also advised to test them on our systems with one we create prior to turning them in - pretty straightforward.
Issue is, I cannot seem to get this to work. I've never done it before, but I've used .bat files to compile and run C++ programs, as well as using makefiles on a unix system, so I feel like I'm absolutely stupid for not figuring this out on my own, but none of my searches have returned any fruitful solutions that help at all.
My program consists of 3 .java files:
Shipment.java - an interface that contains abstracted methods that are implemented in the ShipmentHW1 class
ShipmentHW1.java - a class that implements the abstracted methods from Shipment and has constructors, etc to create a usable object
TestShipment.java - the main class of this program, which utilizes and creates ShipmentHW1 objects based on preset parameters. This is super duper basic stuff here, and again, it runs perfectly fine inside the NetBeans IDE.
The instructions given to us state to have the batch file inside the package directory (which in this case I've set aside a seperate folder on my desktop titled "shipping", which is the package name - shouldn't be any issues there), where the 3 .java files are located as well.
They say if you don't need to explicitly list the path to the JDK, then you can simply have
javac TestShipment.java
java TestShipment.java
pause
Afterwards I get errors talking about how it "cannot find symbol Shipment s = new ShipmentHW1();"
I've tried adding imports, but since they're in the same package it shouldn't even be an issue.
Directory path is
C:\Users\X\Desktop\shipping
All 7 files are contained within:
TestShipment.java
TestShipment.class
Shipment.java
Shipment.class
ShipmentHW1.java
ShipmentHW1.class
doHW1.bat
Does anyone have any idea? I can provide more information if I've been too vague
Also, I'm on Windows 8 if that makes any difference
Solved
Batch file now reads
javac TestShipment.java Shipment.java ShipmentHW1.java
cd ..
java shipment.TestShipment
pause
and it works like a charm. Anyone have any ideas why I had to call the package.class instead of just compiling it regularly?
Try doing
javac TestShipment.java
java TestShipment
pause
Without seeing the contents of TestShipment.java, I'll assume you have some dependency on the Shipment and ShipmentHW1 classes. As such, when you execute a program that uses the TestShipment class, you need to have the .class files for each of the three (and any other dependencies).
So you will have to compile Shipment.java and ShipmentHW1.java as well before running your java command. If they are in the same package, you're good, if not, you will have to specify an appropriate value for the -cp option.
When running java with a class name, you need to specify the fully qualified class name.
If your .java files are declared to be in the 'shipping' package, then you probably need to be running java from the parent directory of 'shipping', e.g.
cd <path>/shipping
javac TestShipment.java
cd ..
java shipping/TestShipment

Not using any kind of javafx application still it says extend javafx

I am using jdk 1.8 , jre 1.8 and eclipse juno.Whenever i use to run my program with eclipse it gives me this error
Error: Main method not found in class A, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
my basic program is
public class A {
public static void main(String[] args) {
System.out.println("Hello");
}
}
Then i tried to run my program from CMD on the first time it run successfully after that it also start giving me this error..Points to remember
When i create a new project i use default jre.which is jre 8.
I am not using any kind of javaFx Application.My basic program is in front of you all
I cant every time extend javafx .So i need a strong way to resolve it.
I run a program from the command prompt for the first time it run ,but when i run another program with little bit of change it did not run.
I am stuck here guys please give me a solution for that and a little bit of explanation for that will be appreciated..
Please, close your cmd prompt.
And reopen cmd prompt.
Then open your directory containing pom.xml
To build your project, Type : mvn install
Then run the java code. For eg,
java -cp target/sample-0.0.1-SNAPSHOT.jar com.maven.sample.HelloMaven
Here HelloMaven is the class name.
I had the same error. Imported java.lang.String and the error went off.
import java.lang.String;

How to find main() function in a big project

I never used Java, but I'm looking over a big server project writting in Java with Eclipse.
My question is, how can I find the main() function, is there an easy way, or I have to search for it in every .java file ?
You will have to do a code base wide search as it could be in any file. Having said that, many server based project do not have a main() function at all. The server provides the infrastructure and only looks for classes which inherit from specific other classes or implement certain interfaces or are mentioned in certain config files. All of this depends on the server and the technologies employed.
You need to find out what your server is, what technologies have been used in the java project and how it was setup.
Use eclipse's build in search function and search for "main(" in all projects java files (= entire workspace)
Look for the application jar and look at it's manifest file, it may contain the name of the main class
Look for scripts that are used to start the application. You eventually find the java call that starts the application (a parameter is the main class)
Look for build scripts (build.xml). Eventually they contain some jar, install or deploy target where startup scripts are autogenerated or manifest files are written. The main class should be named there.
BTW - If the big server project is a server based application, say the final build result is a war or an ear file, then the entry point doesn't have to be a static main method. Then it might not even have one single starting point.
Well, if nothing else, you could use the Search menu, Java, and use "main(String[])" as the search string. Search for: Method, Limit To: Declarations and Search In: Sources. That will help your search.
If you have a run configuration in Eclipse, you can look in that and see which class and method is being invoked.
In your project you may have more than one main() method. So, you should search it in files. Just find main which is declared as public static void.
If you use an ECLIPSE you can try to search with special ECLIPSE search features (menu Search -> Java...).
You better know where it is, because with Java, you can have several main() for one project.
I sometimes have one that runs the project as an application (standalone) and another that runs it as an applet.
The best practice is: know where it is, put it in an obvious place (like in the Main.java file).
Under eclipse Package Explorer right click on project name -> Run As -> Java Application
Eclipse will automatically search and execute main method i.e. public static void main(String[] args) {...}

Categories