I've been looking around for days trying to find a way to compile a whole directory that doesn't only contain Java classes. I've got no idea how to add text files and dependency to the compiling process. Does someone know a way to do that? I know JavaCompiler is a thing but I can't get it to do that. I'd really appreciate it if anyone could send me some sample code using anything able to do what I want.
If you just want to compile the java files in a directory/folder, you can open a terminal , go to the specific folder and run
javac *.java
If you want to do this from your java application, you can use the Runtime class to execute operating system commands from your java program.
For example you can create a bash script or .bat file, put there all your commands, and then call
Runtime run = Runtime.getRuntime();
Process process=runtime.exec("Compile.bat");
You could also create a pom.xml, and use maven to build your project (by calling maven from you application)
Related
I have a class, which has a main, which I'm trying to set as a VirtualMachine's agent.
I could extract it as a jar in eclipse, and add it to the build path, everyt time I change it, but that would be a drag.
this:
File f = new File("/FinalProject/src/finalproje/agent.java");
vm.loadAgent(f.getAbsolutePath());
fails to work.
Is there any way to either make this work, or programmatically compile the .jar file from agent.java?
You can use JarOutputStream. I have used this before and it will work for what you are looking to do. Read this answer HERE
Good luck
I tried to run FindBugs in command line and had troubles when specifying the project to be analyzed. I understand FindBugs works on bytecode (.jar, .class), so I wrote a HelloWorld program and made sure that it had some messy code that would be detected by FindBugs.
Then I tried:
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld/bin
which threw an exception:
java.lang.IllegalArgumentException: Can't read project from HelloWorld/bin
at edu.umd.cs.findbugs.Project.readProject(Project.java:774)
I also tried .class and .jar files, but nothing showed up:
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld/bin/Main.class
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld.jar
I checked the FindBugs manual about the command line option "-project", it says
The project file you specify should be one that was created using the GUI interface. It will typically end in the extension .fb or .fbp
I don't understand this. Does it mean that some pre-processing is required and FindBugs cannot check arbitrary .jar or .class or project directly? How can I get this .fb or .fbp extension?
Thanks.
The procedure is described on the FindBugs website:
Make sure you download the FindBugs distribution which includes the GUI (called Swing interface).
Extract your downloaded ZIP and add its bin folder to your PATH.
Type findbugs to open the GUI, then click New Project
In the dialog:
Enter a project name, say HelloWorld.
Where it says Classpath for analysis, give it the Jar with your .class files or a directory where the .class files are (such as build/classes/main or whatever; the package structure must start in this directory).
Where it says Auxiliary classpath, list any libraries required to load your classes.
Source directories works just like Classpath for analysis, but for .java files. FindBugs uses this to show you where in the code your issues are.
You can select (cloud disabled) as bug store.
Click Analyze.
Now you can save the project configuration as a .fbp project file.
Next time, you can start the analysis by running
java -jar D:/findbugs-2.0.3/lib/findbugs.jar -project HelloWorld.fbp
If you don't want to or cannot use the GUI, you can get the text-only version by adding the -textui option as first option after findbugs.jar. Output formats and behavior are configured via additional command line options.
However, most people use FindBugs integrated with their IDEs, or as part of a build process. Neither use case should require the GUI or command line versions. Take a look at the plugins for your IDE, it may save you a lot of time and they are really easy to use.
I think I have seen this done, but am not sure where. What I want to do is to create a bat file I can package with my class files when sending to a friend to show them progress/ask advice on non programming matters. My friend is not very handy when it comes to code and doesn't like changing computer settings. Just using java myClass as a command line won't work here because although my friend does have java installed, he has not set his windows environment variables so his command prompt knows where to find java.
What kind of line would I need to add to my batch file to make it so it can compensate for problems like this?
Create a manifest file (manifest.txt):
Main-Class: com.mycompany.myapp.MyMainClass
Package your app as a jar:
jar cfm myjarfile.jar manifest.txt *.class
Create a batch file:
start myjarfile.jar
If it is about sharing and running a single java file without jar dependencies. And you are only worried about the java runtime environment setup, then you can use online java code compilers and executors. Here is one:
http://javalaunch.com/JavaLaunch.jsp
You can google for more!
Use an IDE, NetBeans or eclipse and package your files as a Jar file.. that can be executed directly and you do not need to worry about dependencies, other classes or libraries.
COMPLETE EDIT BUT SIMILAR PROBLEM
What's the best software/plugin to enable FTP on Eclipse? I'm using FileZilla, but is there something better/easier?
You are telling javac to compile gamedata.txt and it is reporting an error that it cannot compile this file.
I'd highly suggest using a tool like Ant to script your compilation/packaging/etc so you don't have to worry about typing in arguments on the command line.
First of all, the -J command line argument is not meant to be literally passed as -J<flag>. Taken directly from the javac man page (you can view the exact same thing by typing man javac into the shell):
-Joption
Pass option to the java launcher called by javac. For example,
-J-Xms48m sets the startup memory to 48 megabytes. Although it
does not begin with -X, it is not a `standard option' of javac.
It is a common convention for -J to pass options to the underly-
ing VM executing applications written in Java.
Really, if you want to make this an executable, you can just use the tools that exist in Eclipse to make an executable. Using the command-line javac adds an extra level of complexity that is unnecessary, and that Eclipse is specifically designed to remove.
In eclipse, you can (I think) use File->Export->Java->Executable JAR File to make your project into an executable JAR that any computer with the Java Virtual Machine can run. That way, your project will work on both your computer and the Unix system at your school. You may have to add gameData.txt manually to the JAR or include it separately in the package, not sure how Eclipse does that type of thing though.
You can only compile .java files. If you remove the .txt file from the list of files to compile, it should work fine. If you want to compile all the files in a directory, you can simply use javac *.java
There are some examples in the javac synopsis.
Edit: Updated link to Solaris examples, which are similar to Linux.
I have a java project which contains multiple classes spread out over multiple files.
How do i compile and get this to work ?
Can anyone suggest how to compile this using jCreator (without using a build tool like ant)
Without using Ant/Maven etc. (and I would strongly advocate using these - a command line is unmaintainable as your project increases in complexity, and unless you script it you will have to remember how you invoked it last time when you next build) you should be able to pass all your .java files to the compiler on the command line. e.g. in Unix:
javac `find . -name \*.java'
or similar (you will likely need additional args for the classpath etc.)
You should just be able to compile each file separately and then run the class with the "main" function ie the one that starts of your program. Its probably a good idea to compile the ones that don't depend on any others first so that when each ones compiled all the classes used in that class are already compiled but I don't know if you need to do this.