Convert .java to .class through .bat file

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1005 Views - Last Post: 26 June 2012 - 02:05 PM Rate Topic: -----

#1 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Convert .java to .class through .bat file

Posted 25 June 2012 - 02:24 PM

**This topic might go under the ASM category but last time I placed a similar question and it was moved here (plus the ASM category never gets any attention) Regardless, feel free to move if you want.**

How could I create a .bat file that converts/compiles all .java files in a directory to .class files. After this conversion, it places the .class files in a certain package in a jar. I know very little asm so any help is greatly appreciated.

Or even is there a way to make a jar that does this?: Convert text files to .class files and store in a package in a jar

This post has been edited by Air Legend: 25 June 2012 - 02:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Convert .java to .class through .bat file

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,455
  • Joined: 27-December 08

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 02:33 PM

This really isn't an assembly question, more of a batch question. I'll move this to Other Languages.
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 02:50 PM

The answer to this is that you would place the relevant javac and jar commands in the batch file. If you're more specific, we can get more specific
Was This Post Helpful? 0
  • +
  • -

#4 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 02:59 PM

Honestly this is not that complicated of a question, literally:

"How could I create a .bat file that converts/compiles all .java files in a directory to .class files. After this conversion, it places the .class files in a certain package in a jar. I know very little asm so any help is greatly appreciated."


In other words I would like a .bat file in which, after executed, compiles all .java files in a directory to .class files. How does it know where this directory is? It will read a text file with the path. After it compiles it places the classes in a certain package in a jar. How does it know which package? There is another text file with that path.

So it is a two step process.

1.) Compile
2.) Place in jar

@ECHO OFF

TITLE test

SET cc=javac
SET cflags=
SET scripts=Scripts\Sources
SET scriptspre=Scripts\Precompiled
SET jarpathfile=Settings\path.txt

IF NOT EXIST "%jarpathfile%" (
	ECHO Path file missing. Try restarting the client.
	GOTO end
)

FOR /F "delims=" %%G IN (%jarpathfile%) DO SET jarpath=%%G

CALL FindJDK.bat

IF NOT EXIST %scripts%\*.java (
	ECHO No .java script source files found.
	GOTO end
)

ECHO Compiling scripts
ECHO. > "%scripts%\.class"
DEL /F /Q "%scripts%\*.class" > NUL
"%cc%" %cflags% -cp "%jarpath%" %scripts%\*.java

:end
PAUSE
EXIT



@ECHO OFF

ECHO Looking for JDK

SET KEY_NAME=HKLM\SOFTWARE\JavaSoft\Java Development Kit
FOR /F "tokens=3" %%A IN ('REG QUERY "%KEY_NAME%" /v CurrentVersion 2^>NUL') DO SET jdkv=%%A
SET jdk=

IF DEFINED jdkv (
	FOR /F "skip=2 tokens=2*" %%A IN ('REG QUERY "%KEY_NAME%\%jdkv%" /v JavaHome 2^>NUL') DO SET jdk=%%B
) ELSE (
	FOR /F "tokens=*" %%G IN ('DIR /B "%ProgramFiles%\Java\jdk*"') DO SET jdk=%%G
)

SET jdk=%jdk%\bin
SET javac="%jdk%\javac.exe"

IF NOT EXIST %javac% (
	javac -version 2>NUL
	IF "%ERRORLEVEL%" NEQ "0" GOTO :notfound
) ELSE (
	GOTO :setpath
)
GOTO :eof

:notfound
ECHO JDK not found, please download and install from:
ECHO http://java.sun.com/javase/downloads
ECHO.
PAUSE
EXIT

:setpath
SET PATH=%jdk%;%PATH%
GOTO :eof



This is what I have. Again, my knowledge on bat files and asm is limited so much of this was ripped from various google searches (although I did write some of it myself).

This post has been edited by Air Legend: 25 June 2012 - 03:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 03:01 PM

Why use a bat file? If you know Java, just write a java app that takes a few command line params and does what you want. Even make it a GUI for choices.

To teach you the internals of batch files to do what you want, you could write an app faster than learning it. Google batch file commands/samples

Oh, Assembly has nothing to do with Batch files!!
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 03:04 PM

Using a batch file that's parameterized in such a way is not easy, as your preliminary code shows. You could probably write a proper program with fewer lines.

The best way to control this kind of thing is to use the build tool 'Ant'
Was This Post Helpful? 0
  • +
  • -

#7 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 03:08 PM

View PostGunnerInc, on 25 June 2012 - 03:01 PM, said:

Why use a bat file? If you know Java, just write a java app that takes a few command line params and does what you want. Even make it a GUI for choices.

To teach you the internals of batch files to do what you want, you could write an app faster than learning it. Google batch file commands/samples

Oh, Assembly has nothing to do with Batch files!!


I think a bat file would be the most simple way of doing this. In order for me to do this in java, I would have to make a java compiler in java which is kind of complicated. This question again is a "small one" meaning I don't want a GUI just a "one-click-and-be-done" process.


"Oh, Assembly has nothing to do with Batch files!!"

Thanks, I actually thought batch files were written in asm :P shows how little I know about them haha

View Postg00se, on 25 June 2012 - 03:04 PM, said:

Using a batch file that's parameterized in such a way is not easy, as your preliminary code shows. You could probably write a proper program with fewer lines.

The best way to control this kind of thing is to use the build tool 'Ant'


I am unfamiliar with 'Ant,' would you mind expanding on your different approach?
Was This Post Helpful? 0
  • +
  • -

#8 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 03:09 PM

Why would you have to make a java compiler? All Assemblers/Linkers/Compilers take command line parameters to tell them what to do. Your IDE passes command line params to the above mentioned programs to do their thing.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 03:17 PM

http://ant.apache.or...rldWithAnt.html

If you're going to use Windows scripting, for goodness' sake use Powershell. Batch files have to be one of the worst kinds of scripts of all time for anything more than something very simple
Was This Post Helpful? 0
  • +
  • -

#10 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 03:58 PM

Can one of you simply tell me how to fix the above code to make it accomplish what I'm saying?

I feel like I am pretty close.

View Postg00se, on 25 June 2012 - 03:17 PM, said:

http://ant.apache.or...rldWithAnt.html

If you're going to use Windows scripting, for goodness' sake use Powershell. Batch files have to be one of the worst kinds of scripts of all time for anything more than something very simple


This is VERY simple. I just don't have the knowledge to make something even this simple with a .bat file.

This post has been edited by Air Legend: 25 June 2012 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 04:02 PM

Now you need to start the compiler with the files. So you need some batch commands.

I won't do it for you, but here is a link to commands for batch files

Er, how can you say something is simple if you don't know how to do it or figure it out? Hell, I can say it is simple to run a nuclear power plant, sure for someone with experience just not me.
Was This Post Helpful? 1
  • +
  • -

#12 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 04:07 PM

View PostGunnerInc, on 25 June 2012 - 04:02 PM, said:

Now you need to start the compiler with the files. So you need some batch commands.

I won't do it for you, but here is a link to commands for batch files

Er, how can you say something is simple if you don't know how to do it or figure it out? Hell, I can say it is simple to run a nuclear power plant, sure for someone with experience just not me.


It has been done before and the code I posted was actually all it took to complete the task but for some reason when I copy and modify it slightly for my purposes, it doesn't work (this is what makes me think I'm close).

Also just in thought: "Convert .java to .class and move to a different location" doesn't sound that hard...especially since the first part is accomplished with
javac foo.java

Was This Post Helpful? 0
  • +
  • -

#13 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 04:13 PM

Did you even look at the link I gave you for the batch commands?? Is it easy for me? Yes, I have been using Windows since 3.1 and batch files were used to get repetitive things done.

I am trying to push you to learn to do it yourself. All the commands are on that page with descriptions and how to use them.
Was This Post Helpful? 0
  • +
  • -

#14 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 04:24 PM

View PostGunnerInc, on 25 June 2012 - 04:13 PM, said:

Did you even look at the link I gave you for the batch commands?? Is it easy for me? Yes, I have been using Windows since 3.1 and batch files were used to get repetitive things done.

I am trying to push you to learn to do it yourself. All the commands are on that page with descriptions and how to use them.


No I did and will. Sorry for not thanking you in the precious post, I got a little side-tracked. But that will be very useful (I +'d the post). I appreciate you also pushing me to learn which is usually more important and I do the same with others. The only thing is I do not intend to learn batch files as my expertise is mainly in Java, C#, etc. The only reason I ask this question is I am making a unique Java program for learning purposes and I need this one last part to continue. I understand if you do not wish to provide any source for this one part but I would be very grateful. Again, thanks for all your help.

This post has been edited by Air Legend: 25 June 2012 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

#15 Air Legend  Icon User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 101
  • Joined: 28-November 11

Re: Convert .java to .class through .bat file

Posted 25 June 2012 - 05:40 PM

"Is it easy for me? Yes, I have been using Windows since 3.1 and batch files were used to get repetitive things done." Would you consider creating one and maybe explaining some parts (preferably where I went wrong in the above)?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2