I am trying to veer away from writing most of my Java code in Eclipse and I'm coming up with small projects for myself to work on in order to a. become a better Java programmer and b. learn how to organize my applications (in both terms of code and directory structure).
I noticed that I make plenty of small, incremental changes to classes then to recompile and run my whole application. I'm slightly familiar with Makefiles from a course I took in C++, and I'm less familiar with Build Systems (Maven, Ant). Regarding this mater, here are a few things I'd appreciate help with:
First of all, is there a way that I can write a little file that separates where my .class files are saved during javac *.java?
Say I want to put all my .class files in a /bin folder
In that file, is there a command I can write to simplify the javac *.java and java [class name here...] process?
I know that this process is relatively simple already, but what I want to know is can I write something that will allow me to run commands along the lines of myExec build (compile and save all my java files) myExec run (run my application)
Finally, is there a simple Build System that I can/should learn to use that will allow me to accomplish this? Or am I confusing the point of a Build System.
Look at Maven or Gradle, it automatically separates the source file and class files.
Manual build script writing is waist of time really.
Related
I'm trying to find a way to convert a dll to a jar file. I have a .net application that communicates with a java application. The core entities are .net objects which I have to duplicate manually in java.
I've read about IKVM but it seems that it converts only jars to dlls and not the other way around.
Edit: If there is a tool that creates java classes from a dll it is also fine.
Thanks in advance
There isn't such a tool.
A dll is a natively compiled library. That means its been compiled down to machine code. Probably compiled by a C/C++/C# compiler.
A jar file is a zip file that contains '.class' files, which are files compiled down to 'java virtual machine code'. Probably compiled by a java/clojure/scala compiler.
These are two very different incompatible things.
It's not impossible to create such a tool that would do this translation, but it would definitely be an extremely difficult task, as it would entail translating from one machine code, to another, and would need to manage multiple issues like dependency solving, different type structure etc.
HOWEVER, I'm imagining that you want to do this because you want to use a DLL within some java code. That is somewhat possible, but is actually quite complicated. You'll need to use the JNI.
Take a look at this question as it might help you achieve what you want to do:
Calling C++ dll from Java
This is actually an easy task to perform. Converting .dll to .jar is as simple as using com4j and a couple of commands on the command line.
Download com4j.
Open command line and navigate to com4j directory in above step.
Execute below command.
java -jar tlbimp.jar -o outputFolder -p nameOfPackage "pathToFile"
Then jar the results with the following:
jar cf desiredJarName.jar folderYouWantJard
I have a 202 instructor who says that he feels its fine to use netbeans or eclipse but that for the final project he wants a file that he can load on his xp (I'm not sure why xp) machine, compile and run with the following commands:
javac *.java
java FinalProject
Up to this point I have been editing in a simple text pad like program and missing netbeans but to be fair I can't figure out how to code in netbeans in such a way that I get a generic set of files with no handily added code. If anyone could tell me how to convince netbeans that I don't need packages, ant build, team work software, and a bunch of netbeans helpful files lying around in my code I would really really appreciate it.
First you must get better understanding about IDE. Refer here
for NetBeans and Eclipse
When we write a simple program in few lines we can do with notepad and compile with javac. If we go for a big project there are lot of stuff and features required like
Adding external lib
UI Frame work
Identifying syntax error
Easy compilation, debugging and execution
Writing unit test etc.,
In netbeans, you just take "src" folder and use it.
So with netbeans all the source code files get put into a src directory. This contains nothing other than source files. In order to compile it using javac *.java you should place all your java files in the <default package> or without a package(Netbeans will warn that this is bad practice but you can ignore that for now). To run the program using java FinalProject you need to make a java file called FinalProject.java(with a class called FinalProject. You can create any additional classes in external files as long as they all go in the same <default package>.
Netbeans' scripts will just make it easier for you to compile and test your code. And to submit you just need to submit all the files in the src folder.
Also, as a side note, If you are creating GUI's using netbeans, you should probably use GridBagLayout rather than the default layout as that adds an additional library that will mean that your instructor's compilation will fail.
Background
I am writing a program that will do some bulk renaming of members and functions in a directory of java source code to de-obfuscate the code based on a look-up table .csv file passed in to the program.
What this is for is the source code I have was written against a obfuscated jar. I have a de-obfuscated version of the jar that was run through a customized version RetroGaurd and I would like to parse the mapping file that was passed in to RetroGaurd to de-obfuscate the function calls my source code makes in to the jar. If I just compile my code and run it through RetroGaurd too when I decompile I loose all of my nice commenting and formatting (unless there is a option on RetroGaurd that I missed).
Problem
I found the Abstract Syntax Tree parser built in to Eclipse and it looks perfect for my uses, however I am not planning on writing my program as a plugin for Eclipse, this is going to be a stand alone jar that can be run on any machine.
My main concern is as I write my code I am getting a lot of dependencies on internal jars that Eclipse uses. I know that if I conform to the EPL for the library jars I will have no issues distributing it, but I am concerned about this project getting bigger and bigger as I write it as more and more jars from Eclipse's SDK are required.
Are there any other projects out there that would give me the ability to parse Java source code to do find and replace reliably like AST will allow me to, or is there a way to use RetroGaurd (or a program like it) to run the same de-obfuscation but keep my comments and functions the same without needing to run the de-obfuscated program though a de-compiler afterwards?
If you're worried about the need to run Eclipse GUI to execute your code, then you could consider running the plugin in headless mode. This would allow your plugin to be run from command line. See this SO thread.
You could also use any other open source java compiler. For e.g., openjdk's Java compiler. Please refer to the Resources section.
Hope it helps.
How can I create a Makefile to compile my Java program with a single source file located in src/Hello.java to ouput a file that I can run with ./prog
(1) Take a look at http://ant.apache.org/ - that's a build tool suited to Java better than make. For example check out the javac command in ant.
(2) You have to run java programs by running the java virtual machine (java) and by telling it what to execute. There is no ./xxx way to run a program in java; that method executes scripts or executable programs, and a compiled java program is bytecode which is neither. What you need is to create a little shell script, call it "Hello", give it execute permissions with chmod, its contents should be something like:
#!/bin/bash
java -cp . Hello
Make is not a great tool for building Java programs, primarily because its core design is to identify out-of-date sources (either source or object files) and apply transforms to them. The Java compiler handles this dependency checking already, and defining a Make rule for Java compilation is more trouble than it's worth.
Adrian Smith has already suggested Ant. I'm going to suggest Maven as an alternative.
My main reason for this suggestion is that it's very easy to get a simple project going in Maven: you can tell Maven to create the entire directory structure, and then it just works. And although you may not like the directory structure that Maven creates, there's a benefit to the consistency if you're working in a large project. Maven does have its limitations, and some of them are extremely painful, but you generally won't run into them until you have the knowledge to work around them.
As for running a Java program, you need to invoke the JVM somewhere along the line. A shell script is one approach, but generally it's easier just to invoke the JVM directly. If you create an "executable JAR" (which Maven will do for you, including options to include all dependencies), you can invoke it like this:
java -jar executable.jar
I have recently learnt how to program in java, I was looking at some open source programs from sourceforge.net and after downloading these programs I don't understand the file structure most of the programs follow. Pretty much every program has src,bin,lib etc folders, how do I know the standard way of organizing my program. Is there any book or resource which explains this? also how do I compile this source code once I have downloaded it, to make a jar file from it
thanks
src is (usually) what it sounds like: source code
bin is (usually) shell scripts related to the product
lib is (usually) external dependencies needed for compilation
Most projects document how they build: look for a README or grok the project website. These days for a Java-centric project I would expect Ant or Maven.
What you have downloaded is actually a distribution version of the program. The directories as listed by carej are kind of a convention, mainly derived from how things are usually done on a Unix system.
Most projects supply some manual on how to build. This can be tricky if not all libraries (jars) used by the program are supplied in the distribution. Some of the Apache Commons project do it like this, thus forcing the user to download dependencies seperately.
If you just want to use the program try to find a binary version. This will usually consist of jars, scripts and documentation. Source distributions are useful if you want to look at the source and/or make modifications.